Define and Use the Functions
Explore how to write and use JavaScript functions to organize code into reusable modules. Understand function definition, calling, parameters, and return values. Practice creating functions like multiplication tables, distinct element filters, string searches, and Fibonacci sequence generators to reinforce foundational programming skills.
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 (
A—Zanda—z) and the underscore symbol (_). It may also contain digits (0–9), but the function name can’t start with those. For example,Key_2is a valid function name, but2_keyis not. Function names are case-sensitive, meaning thatname,Name, andNAMEare three different functions.
There are two main parts of a function definition:
-
Header: The first line, with a pair of brackets.
-
Body: The block of statements with the function boundaries.
The function header contains the function, the function name sayHello, the parameters in (), and a pair of curly brackets to indicate the start and the end of the body. All the statements in the function body are written as a block of code enclosed in { and }. We can not write the body of the function without braces.
Let’s write code for the above example.
In the last line, we’re telling the program to run the function sayHello(). This line is known as a function call or calling the function.
Example of function calls
Let’s look at the following code:
In the program:
-
We define two simple functions—
firstandsecond. -
We demonstrate the sequence of execution with the help of
console.logstatements.
The return statement
The following slides illustrate another function that returns a value. The example shows (1) how to use the return statement in the function body and (2) how to write the parameters (a,b) in the function header.
Let’s write out the code for the above example.
In the above code:
-
The last line calls the
console.log()function. It invokes thegetSum()function with the values5and6as arguments for the parametersaandb, respectively. -
The variable,
mysum, holds the sum ofaandb, which is11. -
The statement,
return mysum;, returns11to theconsole.log()statement as a result of the function call.
Note: Any statement written in the function body after the
returnstatement is unreachable.
Practice creating and calling functions
The following are a few example programs to practice creating and calling functions. By clicking the “Show Solution” button, you’ll find a program that solves the respective problem. You can copy and paste the given solution into the code widget to make sure that the output of your solution matches the given solution. There may be several ways of writing correct solutions in programming.
Function to create a multiplication table
Write a function showTableOf4() that displays 20 terms of the table of 4. Call your function to display the results.
Sample output
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
4 x 11 = 44
4 x 12 = 48
4 x 13 = 52
4 x 14 = 56
4 x 15 = 60
4 x 16 = 64
4 x 17 = 68
4 x 18 = 72
4 x 19 = 76
4 x 20 = 80
Function to find distinct values
Write a function showDistinct that takes a list as a parameter and prints the distinct elements in the array. It’s assumed that the array is already sorted in ascending order. Call your function to display the results.
Sample input 1
[2, 5, 5, 8, 8, 8, 9, 30, 45]
Sample output 1
Original array: 2, 5, 5, 8, 8, 8, 9, 30, 45
Distinct array: 2, 5, 8, 9, 30, 45
Sample input 2
[2, 5, 5, 8, 8, 8]
Sample output 1
Original array: 2,5,5,8,8,8
Distinct array: 2,5,8
Function to search a string in an array
Write a function that checks if a string is present in the array. If it’s present, display its index in the array. Call your function to display the results.
Sample input 1
-
First parameter: array
['2','55','888','9','30','45'] -
First parameter: string
'888'
Sample output 1
888 is found at index 2
Sample input 2
-
First parameter: array
['2','55','888','9','30','45'] -
First parameter: string
'50'
Sample output 2
*** 50 is NOT FOUND in the array ***
Function to display the Fibonacci sequence
A Fibonacci sequence is a sequence in which each number is the sum of the two preceding ones. Write a function, fibo, that receives the parameter n to specify the number of terms of the Fibonacci sequence. That function will return a list containing the sequence. Call your function to display the results.
Sample input
10
Sample output
First 10 terms of Fibonacci sequence are: 0,1,1,2,3,5,8,13,21,34
*** End of generating Fibonacci Numbers ***