Calling Functions

Function Parameters

🙋 Need help? Ask an expert now!

To pass information to a function, you can use parameters (also known as arguments). As shown in previous examples, not all functions need to have parameters. Each parameter has a name and are assigned to particular values when the function is called. Parameters change the function behavior depending on the values that are passed. There parameters can be of any type, Number, String, Object, Array, Function, etc. There are no limits to how many parameters a function can have.

To call a function and pass values to its parameters, we use the function's name, followed by a list of expressions for each parameter. These parameters belong inside the set of parenthesis that come after the function's name.

The function above has the parameter number. This parameter is used inside the function, and is considered a local variable. Using the variable number outside the function will cause an error. The function reads the parameter number and runs it through the different if statements to print "Positive", "Negative", or "The number is zero". Expressions can also be used when calling the method as long as the expressions evaluates to a data type that is appropriate for the function.

Edit Me on GitHub!

Application Question

Consider the following code segment:

function alterNumber(number){
              return number * number + 2;

What will the following code segment print to the console?