Skip to main content

Functions in JavaScript

What Are Functions?

Functions are reusable blocks of code designed to perform specific tasks. Think of functions like recipes - they take ingredients (inputs), follow a set of instructions, and produce a result (output).

Functions help you:

  • Organize your code into logical parts
  • Avoid repeating the same code
  • Make your program easier to understand and maintain

Creating Basic Functions

In JavaScript, you define a function using the function keyword:

function sayHello() {
console.log("Hello, world!");
}

// Call the function to run it
sayHello();

When we "call" (or "invoke") the function by writing sayHello(), the code inside the function runs.

Functions with Parameters

Most functions need information to work with. We provide this information using parameters:

function greet(name) {
console.log("Hello, " + name + "!");
}

greet("Alex"); // Displays: Hello, Alex!
greet("Taylor"); // Displays: Hello, Taylor!

You can have multiple parameters separated by commas:

function add(a, b) {
console.log(a + b);
}

add(5, 3); // Displays: 8
add(2, 7); // Displays: 9

Return Values

Often, we want functions to give back a result that we can use. For this, we use return:

function multiply(a, b) {
return a * b;
}

let result = multiply(4, 5);
console.log(result); // Displays: 20

// You can also use the return value directly
console.log(multiply(3, 6)); // Displays: 18

Once a function reaches a return statement, it immediately stops and gives back the specified value.

Function Scope

Variables created inside a function can only be used within that function. This is called "local scope":

function calculateArea() {
let width = 5; // Local variable
let height = 10; // Local variable
let area = width * height;
return area;
}

let roomArea = calculateArea();
console.log(roomArea); // Works: 50

// This would cause an error - width is not defined outside the function
// console.log(width);

However, functions can access variables from outside their own scope:

let multiplier = 2;  // Global variable

function doubleIt(number) {
return number * multiplier; // Can use the global variable
}

console.log(doubleIt(5)); // Displays: 10

Functions as Building Blocks

A key advantage of functions is that you can use them as building blocks, letting each function do one job well:

function calculateArea(width, height) {
return width * height;
}

function calculateCost(area, pricePerSquareFoot) {
return area * pricePerSquareFoot;
}

// Use functions together
let roomWidth = 12;
let roomLength = 15;
let area = calculateArea(roomWidth, roomLength);
let cost = calculateCost(area, 8.50);

console.log("Area: " + area + " square feet");
console.log("Cost: $" + cost);

Arrow Functions

Modern JavaScript offers a shorter way to write functions called "arrow functions":

// Regular function
function add(a, b) {
return a + b;
}

// Same function as an arrow function
const addArrow = (a, b) => {
return a + b;
};

// For simple functions with just a return, you can make it even shorter
const addShort = (a, b) => a + b;

All three functions do the same thing, just with different syntax.

Functions in Conway's Game of Life

Our Game of Life project uses many functions:

  1. Grid Management Functions:
function createEmptyGrids() {
// Creates empty grid arrays
}

function randomizeGrid() {
// Fills grid with random values
}
  1. Game Logic Functions:
function countNeighbors(grid, x, y) {
// Counts neighbors around a cell
let sum = 0;
// ... counting logic ...
return sum;
}

function applyRules(cellState, neighbors) {
// Determines if a cell lives or dies
// ... rules logic ...
return newState;
}
  1. Simulation Control Functions:
function computeNextGeneration() {
// Updates the entire grid for one step
}

function toggleSimulation() {
// Starts or stops the simulation
}

Each function handles a specific task, making our code organized and easier to understand.

Common Function Patterns

Here are some common ways to use functions:

  1. Data transformation: Functions that change data from one form to another
function fahrenheitToCelsius(fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
  1. Event handlers: Functions that respond to user actions
function handleButtonClick() {
// Do something when a button is clicked
}
  1. Helper functions: Small functions that do simple tasks
function isEven(number) {
return number % 2 === 0;
}

Best Practices for Functions

Follow these guidelines to write good functions:

  1. Single responsibility: Each function should do just one thing
  2. Meaningful names: Name functions based on what they do (calculateTotal, not doStuff)
  3. Parameters: Use parameters to make functions flexible rather than hardcoding values
  4. Return values: Have functions return results rather than modifying global variables
  5. Size: Keep functions small and focused - if a function gets too big, break it into smaller ones

Summary

  • Functions are reusable blocks of code
  • They can take inputs (parameters) and provide outputs (return values)
  • Functions help organize code and avoid repetition
  • Variables inside functions have local scope
  • Well-designed functions make code easier to understand and maintain