Lesson 6 of 16
In Progress

Defining and Calling Functions

In JavaScript, a function is a block of code that performs a specific task. Functions are an essential part of any programming language and are used to divide a larger program into smaller, more manageable pieces.

In this article, we’ll go over the basics of defining and calling functions in JavaScript and how to use them in your code.

Defining Functions

To define a function in JavaScript, you use the function keyword followed by the function name and a set of parentheses. The function body is then placed inside curly braces.

Here’s the syntax for defining a function:

function functionName() {
  // code to execute
}

Here’s an example of how to define a function that logs a message to the console:

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

You can also define a function with parameters, which are values that are passed to the function when it is called. The parameters are placed inside the parentheses, separated by commas.

Here’s an example of how to define a function with parameters:

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

Calling Functions

To call a function in JavaScript, you simply use the function name followed by a set of parentheses. If the function has parameters, you pass the values to the function inside the parentheses, separated by commas.

Here’s an example of how to call a function:

sayHello(); // logs "Hello, world!" 
greet("John"); // logs "Hello, John!"

Return Values

A function can also return a value to the calling code. To do this, you use the return keyword followed by the value to be returned.

Here’s an example of a function that returns a value:

function add(x, y) {
  return x + y;
}

You can then assign the return value of the function to a variable:

var result = add(2, 3); // result is 5

Conclusion

Functions are a powerful and useful tool in JavaScript programming. By understanding how to define and call functions, you’ll have the skills you need to organize and reuse your code in a more efficient and effective way.

Exercises

To review these concepts, we will go through a series of exercises designed to test your understanding and apply what you have learned.

Define a function that takes two numbers as parameters and returns the sum of the numbers.

function add(x, y) {
  return x + y;
}

Call the function defined in exercise 1 with the arguments 3 and 4 and assign the result to a variable.

var result = add(3, 4); // result is 7

Define a function that takes a string as a parameter and returns the string with the first letter capitalized.

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

Define a function that takes an array as a parameter and returns the sum of all the elements in the array.

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

Call the function defined in the previous exercise with the argument [1, 2, 3, 4, 5] and assign the result to a variable.

var result = sum([1, 2, 3, 4, 5]); // result is 15