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  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

  • slogan function do not have any parameters because it do not need. Also this function do not return a value just prints to console

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)