Function parameters let you send information into a function so it can use that information to do its job.
Function parameters in C++
return_type function_name(parameter_type1 parameter_name1, parameter_type2 parameter_name2, ... ) {
// function body
}Parameters are listed inside the parentheses after the function name.
Each parameter has a type and a name, separated by a space.
name of type std::string.void greet(std::string name) { std::cout << "Hello, " << name << "!" << std::endl; }
int add(int a, int b) { return a + b; }
void printNumber(int number = 10) { std::cout << number << std::endl; }
This program shows two functions with parameters. printSum adds two numbers and prints the result. greet says hello to the given name.
#include <iostream> #include <string> // Function that takes two numbers and prints their sum void printSum(int x, int y) { std::cout << "Sum: " << x + y << std::endl; } // Function that greets a person by name void greet(std::string name) { std::cout << "Hello, " << name << "!" << std::endl; } int main() { printSum(5, 7); // Calls printSum with 5 and 7 greet("Alice"); // Calls greet with "Alice" return 0; }
Parameters are like placeholders for values you give when calling the function.
You can have as many parameters as you need, separated by commas.
Parameters help make functions flexible and reusable.
Function parameters let you send data into functions to work with.
They are listed inside parentheses after the function name with a type and a name.
Using parameters makes your code more flexible and easier to reuse.
Practice
What is the purpose of function parameters in C++?
Solution
Step 1: Understand function parameters
Function parameters are variables listed in the function definition that receive values when the function is called.Step 2: Identify their purpose
They allow the function to work with different data each time it is called, making the function flexible and reusable.Final Answer:
To send data into functions so they can use it -> Option AQuick Check:
Function parameters = input data for functions [OK]
- Confusing parameters with return types
- Thinking parameters declare variables inside function body
- Believing parameters define function name
Which of the following is the correct syntax to declare a function add that takes two int parameters a and b?
Solution
Step 1: Recall correct parameter syntax
In C++, parameters are declared astype namepairs separated by commas inside parentheses.Step 2: Check each option
int add(int a, int b); correctly usesint a, int b. Options A, B, and C have syntax errors like missing comma, wrong order, or semicolon.Final Answer:
int add(int a, int b); -> Option AQuick Check:
Parameters = type name, comma separated [OK]
- Swapping type and name order
- Using semicolons instead of commas
- Omitting commas between parameters
What will be the output of the following code?
void greet(std::string name) {
std::cout << "Hello, " << name << "!" << std::endl;
}
int main() {
greet("Alice");
return 0;
}Solution
Step 1: Understand function call with parameter
The functiongreettakes a string parameternameand prints "Hello, " plus the value ofname.Step 2: Check the argument passed
Inmain,greetis called with "Alice", sonamebecomes "Alice".Final Answer:
Hello, Alice! -> Option DQuick Check:
Parameter value replaces variable in output [OK]
- Thinking parameter name prints literally
- Expecting empty output if parameter unused
- Assuming syntax error due to string parameter
Identify the error in this function declaration:
void printSum(int a, b) {
std::cout << a + b << std::endl;
}Solution
Step 1: Check parameter declarations
Each parameter must have a type and a name. Here,bhas no type specified.Step 2: Identify correct syntax
The correct declaration isvoid printSum(int a, int b). Missing type causes a syntax error.Final Answer:
Missing type for parameter b -> Option BQuick Check:
Every parameter needs a type [OK]
- Omitting type for second or later parameters
- Assuming return type must be int
- Using wrong brackets for parameters
Consider this function that calculates the area of a rectangle:
double area(double width, double height) {
return width * height;
}How would you call this function to calculate the area of a rectangle with width 5.0 and height 3.0, and store the result in a variable result?
Solution
Step 1: Understand parameter order and types
The function expectswidthfirst, thenheight, both as doubles.Step 2: Check each call option
double result = area(5.0, 3.0); passes 5.0 as width and 3.0 as height correctly. double result = area(3.0, 5.0); swaps them. double result = area(width=5.0, height=3.0); uses invalid named arguments (not supported in C++). double result = area(5, 3); passes ints which convert but less precise.Final Answer:
double result = area(5.0, 3.0); -> Option CQuick Check:
Call with correct order and types [OK]
- Swapping argument order
- Using named arguments (not valid in C++)
- Passing wrong types without conversion
Ready to prove it?
Pass the test and this pattern moves unknown
