We can create and run functions that take a list or instructions and package it into a named command. This is to remove repetitious code and to aid in debugging.
function functionName(){instructions;}
Called by using functionName();
You need the function keyword when you define the function.
The variable naming rules apply to funtions, so they are named using camelCase.
Console.log() -> Shows items inside console.
Practice with Karel -> Link Here
function functionName(input parameter){somecodethatusestheparameterhere;}
functionName(argument);
When using functions, a parameter is found between the (). When you call the function, the argument goes between the ().
function function(stringa){} -> stringa is the parameter. Parameters are used to take in a value or variable which will then be used somewhere inside of the function.
function(stringb); -> stringb is the argument. This will be used as a variable value in the code.
Functions do not always need to have a parameter. Sometimes, they can stay empty.
Functions that can give an output end with a return. This means that the function can both take an input and export an output, and can return a variable or a calculated value.
function getPogs(money){
return money % 2;
}
getPogs(5); //returns 1
Additionally, we are able to use a function (f2) inside of a function(f!), and have our internal function(f2) return a value that is used in the external function(f1). This is useful when we change multiple values and replace them with variable values.
function function1(value){
function2(value);
}
function function2(value){
value+=2;
}
BMI = weight kg / height ^2(m^2)
function bmiCalc(height, weight){
var BMI = weight/(height * height);
console.log("Your BMI is " + BMI );
return BMI;
}