JavaScript Functions

irvan-smith-5eBW5GomfhY-unsplash.jpg

In a programing language called Javascript, you will come across a function. Yes, function. Ok let's pause a little, What comes to your mind when a function was mentioned?

ok fine, function eliminates the need of writing the same code again and again. function allows a programmer to divide a big program into a number of small manageable functions.

Defining Functions

  • function is defined using the word function keyword.
  • followed by a unique function name.
  • A list of parameters to the function, enclosed in parenthesis and separated by commas.
  • And a statement block surrounded by braces.

Function Syntax

function functionName (parameter-list) {
      statements...
}

Declaring a function

so let's declare a function named greet ()

function greet () {
      console.log("Hello!")
}

so for us to call a function, we need to call it from the declared function name call greet ().

// function call
greet();

out put

Hello!

function parameters

yes, you can pass different parameters while calling a function. And parameter is a value that is passed when declaring a function. A function can take multiple parameters separated by comma.

Function with Parameter

// program to print the text
// declaring a function 

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

// variable name can be different 

let name = prompt("Enter a name:");

// calling function
greet (name);

output

Enter a name: Emily
Hello Emily :)

When a value is passed when declaring a function, it is called parameter. And when the function is called, the value passed is called an argument.

Adding Of Two - Numbers

earlier in the discussion of parameters, we learnt that a function can take multiple parameters separated by comma.

// program to add two numbers using a function 
// declaring a function 

function add (a, b ) {
     console.log (a+b);
}

// calling functions
add (3,4);
add (2,9);

out put

7
11

so the add function is used to find the sum of two numbers.

  • The function is declared with two parameters a and b.
  • The function is called using its name and passing two arguments 3 and 4 in one and 2 and 9 in another.

Notice that you can call a function as many times as you want. you can write one function and then call it multiple times with different arguments.

function Return Statement

well, you can pass two numbers in a function, and then you can expect the function to return their multiplication in your calling program. That means the return statement can be used to return the value to a function call. The return statement shows that the function has ended. So any code after return is not executed. and then returns undefined value if nothing is returned.

Return The Sum of two numbers

// program to add two numbers using a function
// declaring a function

function add (a, b) {
   return a+b;
}

// take input from the user

let num1 = parsefloat(prompt("Enter First number:"));
let num2 = parsefloat(prompt("Enter Second number:"));

// calling function
let result = add(num1, num2);

// display the result
console.log("the sum is " + result);

output

Enter First number: 4
Enter Second number: 4

the sum is 8