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 values1and2are arguments;
- Example: In
- A parameter is a placeholder inside the function definition that receives the argumentโs value:
- Example: In
function sum(a, b),aandbare parameters.
- Example: In
Study More
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?
Everything was clear?
Thanks for your feedback!
Sectionย 4. Chapterย 5
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Sectionย 4. Chapterย 5