JavaScript Function
function add ( x, y ) {
return x + y;
}
c = add ( 20, 35 );
console.log ( c );
function add (x,y)
is the function definition. function
is the keyword. add
is an optional function name. Otherwise, it may remain anonymous. (x,y)
contains comma-separated parameters which will be defined as variables, and initialized to given arguments. In this case there are two, representing two numerical values. Some functions will have empty parentheses. {return x+y;}
contains the body of the function or statements. c = add (20,35);
invokes the function. console.log(c);
prints the result to the console, which reads 55
.