DEV Community

subash
subash

Posted on

What is a Function? Simple Explanation with Examples

what is function ?

1.function is a block of code that perform specific task.
*block of code is a group of instruction to perform specific task.
2.Instead of writing the same code again and again, you can write it once inside a function and reuse it whenever needed.

EXAMPLES;

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

*In this case function is keyword, square is the function name what we named, inside the parentheses have parameters if we want to use so many parameters inside the parentheses then we must separate each of them with commas.

*if we want to execute this function, then we should call this name of function, this function name is square so we call this like square() or if we want to put arguments then we should call this like square(23) in this case what was happened is you imagine number(parameter) is the variable and arguments is variable value , what the value(arguments) we give while callback that value will store in variable(parameters)

for example:
let calculation = square(2) --- => this returned number * number
console.log (calculation)
Then final output is --- 4 (2*2 --- => number*number)

Inside calculation have function that entire function returned (number * number), then we print the variable calculation then output is 4 because my arguments is 2, that 2 will be the value of number so the function return number * number that means 2*2 =>4

REFERENCE WEBSITE;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions

Top comments (0)