Submodule 12.1: JavaScript Functions

  • What are functions?
  • Parameters and Arguments
  • Return

Return

Return is another important statement of functions. The return does what its name suggests, it returns a value (number, Boolean, string). An example of the return usage is:

function myFuncname (myFirstnum, mySecondnum) {
return  myFirstnum + mySecondnum;
} // here we call the function and assign it to a variable var sum = myFuncname(2,4); // you can use console.log to see the result of the return statement console.log (sum);

 

Note that the parameters (myFirstnum, mySecondnum) are what “informs” the function that it is going to receive two arguments, which in this case are 2 and 4. If instead of two arguments we had written one or more than two, the console will have given us an error message.

Exercise

Follow the next link. Right-click, select inspect and view the resunt in the Console tab. Next click the "Sources" tab and click the HTML file to see the code:

  Function returns value