Arguments
Part of the Fundamentals section of Coddy's JavaScript journey — lesson 46 of 77.
An argument is a value you pass into a function when you call it.
A parameter is the variable inside the function that receives that value.
To define a function with parameters, we write them inside parentheses:
function functionName(param1, param2) {
code
}We can name the parameters as we want, and we can write as many arguments as we need.
To call a function and pass arguments to it, we write:
functionName(value1, value2, value3, ...)Example of usage:
function isEven(number) { // "number" is a parameter
if (number % 2 === 0) {
console.log(`${number} is even`);
} else {
console.log(`${number} is odd`);
}
}
for (let i = 15; i < 34; i++) {
isEven(i); // i is the argument
}
for (let i = 153; i < 219; i++) {
isEven(i); // argument again
}Here, number is the parameter (placeholder), and each value we pass (like 15 or 153) is an argument.
Challenge
EasyWrite a program that gets two inputs, numbers. The input numbers are the parameters of the below function.
Create a function that gets two parameters, multiplies them (calculates the product) and prints the result, name the function however you like.
Call the function with the input numbers.
Note! In your code, write the function before it's call/execution statements.
Cheat sheet
Functions can accept parameters (placeholders in the function) and receive arguments (actual values passed in).
Define parameters inside the parentheses:
function functionName(param1, param2) {
code
}Call a function by passing arguments (values):
functionName(value1, value2, value3, ...)Example:
function isEven(number) { // "number" is a parameter
if (number % 2 === 0) {
console.log(`${number} is even`);
} else {
console.log(`${number} is odd`);
}
}
isEven(15); // 15 is an argumentTry it yourself
let a = parseInt(inp[0]); // Don't change this line
let b = parseInt(inp[1]); // Don't change this line
// Type your code below
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Type Coercion7Bill Split Calculator
Welcome MessageCalculating The Tip And Total2Variables
NumbersStringBooleanNaming ConventionsEmpty VariablesRecap - Initialize VariablesConstants3Operators Part 1
Arithmetic OperatorsModulo OperatorArithmetic ShortcutsComparison OperatorsStrict vs Loose EqualityRecap - Simple Math6Basic IO
OutputOutput with VariablesType Conversion - Part 1Type Conversion - Part 2Recap - Till 120Recap - True or False