Passing Data into Functions
We can pass data into functions using parameters:
function funcName(parameter1, parameter2, ...) {
// your code hereβ¦
}
A parameter acts like a local variable inside the function. However, it takes up the value of the corresponding argument that is passed into the function at the function call.
12345678function sum(a, b) { console.log(a + b); } sum(1, 2); // Output: 3 sum(5, 10); // Output: 15 sum(20, 30); // Output: 50 sum(33, 37); // Output: 70
- An argument is the actual value passed into a function when calling it:
- Example: In
sum(1, 2)
, the values1
and2
are arguments;
- Example: In
- A parameter is a placeholder inside the function definition that receives the argumentβs value:
- Example: In
function sum(a, b)
,a
andb
are parameters.
- Example: In
A function in which other functions are called is known as a compound function. A compound function combines smaller functions to accomplish a larger task.
1. How many parameters does this function have?
2. What will be the output of this code?
3. Which of the following is true?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 1.33
Passing Data into Functions
Swipe to show menu
We can pass data into functions using parameters:
function funcName(parameter1, parameter2, ...) {
// your code hereβ¦
}
A parameter acts like a local variable inside the function. However, it takes up the value of the corresponding argument that is passed into the function at the function call.
12345678function sum(a, b) { console.log(a + b); } sum(1, 2); // Output: 3 sum(5, 10); // Output: 15 sum(20, 30); // Output: 50 sum(33, 37); // Output: 70
- An argument is the actual value passed into a function when calling it:
- Example: In
sum(1, 2)
, the values1
and2
are arguments;
- Example: In
- A parameter is a placeholder inside the function definition that receives the argumentβs value:
- Example: In
function sum(a, b)
,a
andb
are parameters.
- Example: In
A function in which other functions are called is known as a compound function. A compound function combines smaller functions to accomplish a larger task.
1. How many parameters does this function have?
2. What will be the output of this code?
3. Which of the following is true?
Thanks for your feedback!