Function Usage

Tutorial 3 of 4

1. Introduction

Goal of the Tutorial

This tutorial aims to provide a comprehensive understanding of functions in JavaScript. You'll learn how to define, call, and use functions in your code, and you'll get to know why they are an essential part of programming in JavaScript.

Learning Outcomes

By the end of this tutorial, you'll be able to:
- Understand what functions are and why they're used
- Define and call functions
- Use parameters and return values
- Understand function scope

Prerequisites

Basic knowledge of JavaScript, including variables and data types, is required to follow this tutorial.

2. Step-by-Step Guide

What are functions?

In JavaScript, a function is a block of code designed to perform a particular task. It is executed when it is called (or invoked). Functions are reusable, which means that the same function can be used multiple times.

Declaring a Function

To declare a function in JavaScript, you use the function keyword, followed by the name of the function, parentheses () and curly braces {}. The code that the function will execute is placed inside the curly braces.

function myFunction() {
  // code to be executed
}

Calling a Function

A function is executed when it is called. To call a function, you simply use the function name followed by parentheses.

myFunction(); // this will call the function

Function Parameters and Return Values

Functions can take parameters, which are values you supply to the function so that the function can do something utilizing those values. These are placed inside the parentheses when declaring the function, separated by commas if there is more than one.

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

Functions can also return a value using the return keyword. This value can then be used elsewhere in your code.

function square(number) {
  return number * number;
}

3. Code Examples

Example 1: A simple function that prints a greeting

// Defining the function
function greet() {
  console.log("Hello, world!");
}

// Calling the function
greet(); // Output: "Hello, world!"

Example 2: A function with a parameter

// Defining the function
function greet(name) {
  console.log("Hello, " + name);
}

// Calling the function
greet("Alice"); // Output: "Hello, Alice"

Example 3: A function with a return value

// Defining the function
function square(number) {
  return number * number;
}

// Calling the function and storing the return value
let result = square(5); 
console.log(result); // Output: 25

4. Summary

In this tutorial, you learned about functions in JavaScript. Functions are reusable blocks of code that can be called multiple times. They can take parameters and return a value. Functions are a crucial part of JavaScript and most other programming languages, making your code more modular and efficient.

5. Practice Exercises

  1. Write a function called 'add' that takes two parameters and returns their sum.

Solution:

javascript function add(x, y) { return x + y; } console.log(add(5, 3)); // Output: 8

  1. Write a function called 'max' that takes two parameters and returns the largest one.

Solution:

javascript function max(x, y) { if (x > y) { return x; } else { return y; } } console.log(max(10, 15)); // Output: 15

  1. Write a function called 'factorial' that takes a single parameter and returns the factorial of that parameter.

Solution:

javascript function factorial(n) { if (n === 0 || n === 1) { return 1; } else { return n * factorial(n - 1); } } console.log(factorial(5)); // Output: 120

Happy coding! Continue practicing to become more comfortable with functions.