functions

A function is a block of code that is called using its function name.

 

Functions are declared using the keyword function, followed by the identifier aka token / function name and zero or more parameters to pass into the function within parentheses.

 

Syntax:

function myFunction(parameters){

...statements...

}

 

To use the function it is called by using the function name .e.g myFunction(parameters) ;

 

This example shows three functions. The first simply calls a function with no parameters. The second function is called with a single parameter (bike) that is passed in to the function, which also uses the global topSpeed variable. Finally the third function also passes in the topSpeed global variable but also returns a value that is used in the last statement:

Save & refresh browser:

If only I could afford one of these!
My Ducati Panigale R has a top speed of 202mph!
Spanking out 67.33mph in just the 1st gear!

Leave a Reply