Functions

A function is a sequence of program instructions that performs a specific task, packaged as a unit. When we create a function, we can name the arguments it takes (but we don't have to). Arguments are variables that we give to the function so that it can operate on them (generally, a function cannot access objects created outside it due to variable scopes, which I will discuss in a moment). Functions follow the same naming rules as variables.

A function can return objects to the outside (variables, numbers, arithmetic operations, etc.) When it does, the function's return value is the result of its "calling." To see this value, we have to display this "calling" in the console using console.log() directly or assign it to a variable and then display it. The return instruction also ends the function's execution. To call a function (run it), we write its name and a parenthesis. If it takes any arguments, we write them inside the parenthesis. The names of the arguments don't have to be the same as the passed variables.


function func() {
    console.log("Function")
}

function addition(x, y) {
    return x + y
}

func()
let x = 2
let y = 4
console.log(addition(x, y))
                                    

// Setting default arguments (we can, but don't have to give them)
function func1(x, y = 1) {
    return x + y
}
console.log(func1(1))
console.log(func1(1, 2))
                                    

Function parameters are "placeholders" defined in a function declaration (function addition(x, y)), while arguments are the actual values passed to the function during its call (addition(2, 4)).

Variable scopes

Not every variable is accessible in every part of the program. Variables created in the main block (outside of any functions) are called global variables. They are accessible in all functions and instructions unless they are shadowed by a local variable with the same name. local variables are variables created inside a block (a loop, conditional statement, function, etc.) and are accessible only inside this block.


let global_variable = 5

function func() {
    global_variable += 1
    console.log("Global:", global_variable)
}

function func2(local_variable) {
    local_variable += 1
    console.log("Local:", local_variable)
}

function func3(local_variable) {
    local_variable += 1
    console.log("Local:", local_variable)
    return local_variable // to see the changes made to a local variable in the outer block, we have to return its value
}

func()
global_variable = 6
func()

let local_variable = 7
func2(local_variable)
console.log(global_variable, local_variable) // 6 7

local_variable = func3(local_variable)
console.log(local_variable) // 8
                                    

Recursive functions

A recursive function is a function that calls itself. Recursion is often used in various algorithms.


function func() {
    console.log("x")
    func()
}
func()
                                    

The rest operator

The rest operator (...) is used to collect multiple elements (e.g., function arguments) into a single array. It can also be used to split the elements of an array into variables, but then its name changes to the spread operator.


function func(...args) {
    console.log(args)
}
func(1, 2, 3, 4, 5)
                                    

function func(a, b, c, d, e) {
    console.log(a, b, c, d, e)
}
let arr = [1, 2, 3, 4, 5]
func(...arr)
                                    

The arguments object

The arguments object is an array-like object available inside all non-arrow functions (I will talk about arrow functions in the upcoming lessons). It holds all the arguments passed to the function.


function sum() {
    let total = 0
    for (let i = 0; i < arguments.length; i++)
        total += arguments[i]
    return total
}

console.log(sum(1, 2, 3, 4)) // the sum of the given arguments