Procedural Programming

Procedural programming is an extension of structured programming, where code is organized into reusable procedures or functions. These functions can take input parameters, perform specific tasks, and optionally return a value. JavaScript is a multi-paradigm language that supports procedural, object-oriented, and functional programming styles. In this chapter, we focus on the procedural part: defining functions and calling them from other parts of your code.

function calculateArea(radius) {
  return Math.PI * radius * radius;
}

const circleRadius = 5;
const circleArea = calculateArea(circleRadius);
console.log(
  `The area of a circle with radius ${circleRadius} is ${circleArea}`
);

Keep in mind that this chapter does not cover everything about functions. We will cover the more advanced topics in the chapter on functional programming.

Learning Outcomes

  • Define functions in JavaScript using declarations, expressions, and arrow syntax
  • Control how arguments and parameters pass data into a function, including default, rest, and spread forms
  • Explain scope and hoisting and how they determine where a function or variable is accessible
  • Explain that functions are first-class objects with their own properties and methods
  • Apply these concepts to organize code into reusable procedures

Sections

  1. Defining Functions
  2. Function Arguments and Parameters
  3. Function Expressions
  4. Arrow Functions
  5. Function Scope and Hoisting
  6. Functions as Objects
  7. Practice Questions