Simple Function
function hello() {
console.log("Hello World!")
}
hello()
Function with Arguments
%%js
//Functon takes two paramters and outputs the product
function multiplication(param1, param2) {
console.log(param1 * param2);
}
//Numbers can be directly added into the parameters
multiplication(5,2) //5 * 2
multiplication(10,10) //10 * 10
//Function takes two parameters and outputs the sum
function addition(param1, param2) {
console.log(param1 + param2)
}
//Variables can also be added into the parameters
let num1 = 5
let num2 = 5
addition(num1,num2) // 5 + 5
Function with Return
%%js
//Function that returns a number times 5
function timesFive(num) {
return num * 5
}
//If the variable is a Number, then output the result
if (typeof num === Number) {
console.log(result)
} else {
console.log("Please Enter a Number")
} // else output "Please Enter a Number"
result = timesFive("Hello") //Outputs NaN
result = timesFive(5) // Outputs 25
Global & Local Scopes in Functions