GAME DEV 2026 · JAVASCRIPT FUNDAMENTALS
t1a09 — Functions by Innes
Functions organize code into reusable blocks. A function can receive information through parameters, perform an action, and return a result.
Example function
function myAddNumbers(myIn01, myIn02) {
const mySum = myIn01 + myIn02;
return mySum;
}
const myAnswer = myAddNumbers(3, 6);
myAddNumbers is the function name. myIn01 and myIn02 are parameters. The values 3 and 6 are arguments passed into the function. return sends the answer back to the code that called it.
Try the function
Output
Enter two numbers and call a function.
More function examples
Functions can also return text, not just numbers. Click the button to call a function that builds a greeting.
More function examples
A function can accept one or several parameters, and can return a calculated value.
function myPower(myBase, myExponent) {
return myBase ** myExponent;
}
function myIsEven(myNumber) {
return myNumber % 2 === 0;
}
Challenge
Create a function that divides the first number by the second. Return a useful message instead of dividing by zero. Then try writing a function that accepts one parameter.