...

/

Define and Use the Functions

Define and Use the Functions

Learn to divide a program into functions or subprograms in JavaScript.

Function

We can divide a program into procedural components or modules called functions in JavaScript. We are already familiar with functions like console.log(), prompt(), and length(). Functions can be copied and reused in a programming technique called a modular or procedural approach, also known as the divide and conquer approach. We should always try to design functions that can be reused in other programs too.

Broadly, there are two types of functions, which we’ll explore in the following sections.

Structure of a function

The following slides illustrate various parts of a function definition:

A function name can only contain letters (AZ and az) and the underscore symbol (_). It may also contain digits (09), but the function name can’t start with those. For example, Key_2 is a valid function name, but 2_key is not. Function names are case-sensitive, meaning that name, Name, and NAME are three different functions.

There are two main parts of a function definition:

  • Header: The first line, with a pair of brackets. ...