Javascript Define and Call Functions – POFTUT

Javascript Define and Call Functions


Up to now we have write out code line by line. But there is problem with that. What will we do if the same 3 line code should be used in different 120 places. Here functions come to play. Functions a block of code that can be used just with single line.

function sum(a,b){
 return a+b;
}
s1=sum(1,2);
s=1sum(4,5);
s2=sum(10,20);
  • function  is the keyword used to specify function
  • sum is the name of the function and will be used to call function
  • (a,b) specifies parameters of the function. Parameter is input value for functions.
  • return is keyword used to give back new value to the function callers. s1,s2,s3 is assigned to the values calculated by function.
  • s1 is assigned value which is calculated by sum function with values 1,2 . s1 will hold sum of 1 and 2 which is 3

Function do not have to return a value. So following function is valid too

function slogan(){
 console.log("Hello Poftut")
}
  • slogan function do not have any parameters because it do not need. Also this function do not return a value just prints to console

LEARN MORE  Linux Chmod Command Tutorial with Examples To Change Permission of Files and Folders

Leave a Comment