Lesson 35 of 60 – Function Parameters in C++
58%

Function Parameters in C++

Function parameters are variables declared in a function definition that allow a function to receive data from the code that calls it. Parameters make functions flexible and reusable because the same function can work with different values.

Note: A parameter is the variable written in the function definition, while an argument is the actual value passed to the function when it is called.

1. What is a Function Parameter?

A parameter is a variable that receives a value when a function is called.

void greet(std::string name) {

    std::cout << "Hello " << name;

}

Here, name is a parameter of the function.

2. Parameter vs Argument

Parameter: Variable declared in the function definition.

void greet(std::string name) {
    std::cout << name;
}

Here, name is the parameter.

Argument: Actual value passed to the function.

greet("Rahul");

Here, "Rahul" is the argument.

3. Function with One Parameter

#include <iostream>

void greet(std::string name) {

    std::cout << "Hello "
              << name;

}

int main() {

    greet("Amit");

    return 0;
}

Output:

Hello Amit

4. Parameter Data Type

Every parameter must have a data type.

void display(int number) {

    std::cout << number;

}

Here, number is an integer parameter.

Other common parameter types include double, char, bool, and std::string.

5. Function with Two Parameters

void display(int age, double height) {

    std::cout << "Age = "
              << age
              << std::endl;

    std::cout << "Height = "
              << height;

}

A function can receive multiple parameters.

6. Passing Two Arguments

void addNumbers(int a, int b) {

    std::cout << a + b;

}

int main() {

    addNumbers(10, 20);

    return 0;
}

The values 10 and 20 are passed to a and b.

7. Parameters with Return Value

int add(int a, int b) {

    return a + b;

}

int main() {

    int result = add(15, 25);

    std::cout << result;

    return 0;
}

The function receives two parameters and returns their sum.

8. Parameter and Local Variable

int square(int number) {

    int result = number * number;

    return result;
}

The parameter number is available inside the function and can be used to calculate another local variable.

9. Passing Different Values

int square(int number) {

    return number * number;

}

int main() {

    std::cout << square(2) << std::endl;
    std::cout << square(5) << std::endl;
    std::cout << square(10);

    return 0;
}

The same function can be used with different arguments.

Output:

4
25
100

10. String Parameter

#include <iostream>
#include <string>

void welcome(std::string name) {

    std::cout << "Welcome "
              << name;

}

int main() {

    welcome("Soopro");

    return 0;
}

A string parameter can receive text data.

11. Character Parameter

void showGrade(char grade) {

    std::cout << "Grade = "
              << grade;

}

int main() {

    showGrade('A');

    return 0;
}

The function receives a single character.

12. Boolean Parameter

void checkStatus(bool active) {

    if (active) {

        std::cout << "Active";

    }
    else {

        std::cout << "Inactive";
    }
}

int main() {

    checkStatus(true);

    return 0;
}

A bool parameter can receive true or false.

13. Multiple Parameters of Same Type

int multiply(int a, int b, int c) {

    return a * b * c;

}

int main() {

    std::cout << multiply(2, 3, 4);

    return 0;
}

A function can have several parameters of the same data type.

14. Multiple Parameters of Different Types

void student(std::string name, int age, double marks) {

    std::cout << "Name: "
              << name
              << std::endl;

    std::cout << "Age: "
              << age
              << std::endl;

    std::cout << "Marks: "
              << marks;
}

int main() {

    student("Rahul", 20, 85.5);

    return 0;
}

15. Passing Variables as Arguments

int add(int a, int b) {

    return a + b;

}

int main() {

    int x = 10;
    int y = 20;

    int result = add(x, y);

    std::cout << result;

    return 0;
}

Variables can be passed as arguments instead of directly writing values.

16. Passing Expressions as Arguments

int multiply(int a, int b) {

    return a * b;

}

int main() {

    std::cout << multiply(5 + 2, 3 + 1);

    return 0;
}

Expressions can also be used as function arguments when their resulting values are compatible with the parameter types.

17. Parameters and Function Declaration

#include <iostream>

int add(int a, int b);

int main() {

    std::cout << add(10, 20);

    return 0;
}

int add(int a, int b) {

    return a + b;
}

The function declaration specifies the parameter types before the function is used.

18. Parameter Names in Declaration

Parameter names are optional in a function declaration when only the types are needed.

int add(int, int);

The definition can use parameter names:

int add(int a, int b) {

    return a + b;
}

The parameter types must match the intended function signature.

19. Default Function Parameters

A parameter can have a default value. If an argument is not supplied for that parameter, the default value is used.

void greet(std::string name = "Guest") {

    std::cout << "Hello "
              << name;

}

int main() {

    greet();

    std::cout << std::endl;

    greet("Amit");

    return 0;
}

The first call uses Guest, while the second call uses Amit.

20. Multiple Default Parameters

int calculate(int a, int b = 10) {

    return a + b;

}

int main() {

    std::cout << calculate(5)
              << std::endl;

    std::cout << calculate(5, 20);

    return 0;
}

Output:

15
25

In the first call, the default value 10 is used for b.

21. Default Parameters Rule

Default arguments are generally specified from right to left in a function parameter list.

Valid:

int calculate(int a, int b = 10, int c = 20);

Invalid example:

int calculate(int a = 10, int b, int c);

A required parameter should not follow a parameter that has a default argument in the same parameter list.

22. Pass by Value

By default, function parameters are passed by value. The function receives a copy of the argument.

void change(int number) {

    number = 100;

}

int main() {

    int x = 10;

    change(x);

    std::cout << x;

    return 0;
}

Output:

10

The original variable remains unchanged because the function modifies its copy.

23. Pass by Reference

A reference parameter can allow a function to modify the original variable.

void change(int &number) {

    number = 100;

}

int main() {

    int x = 10;

    change(x);

    std::cout << x;

    return 0;
}

Output:

100

The reference parameter refers to the original variable.

24. Swapping Two Numbers with References

void swapNumbers(int &a, int &b) {

    int temp = a;

    a = b;

    b = temp;
}

int main() {

    int x = 10;
    int y = 20;

    swapNumbers(x, y);

    std::cout << "x = "
              << x
              << std::endl;

    std::cout << "y = "
              << y;

    return 0;
}

The reference parameters allow the function to change the original variables.

25. const Reference Parameter

A const reference can receive a reference to an object without allowing the function to modify it through that reference.

void display(const std::string &name) {

    std::cout << name;

}

int main() {

    std::string name = "Rahul";

    display(name);

    return 0;
}

This is commonly useful for passing larger objects such as strings without making an unnecessary copy.

26. Common Mistakes with Parameters

  • Passing the wrong number of arguments.
  • Passing values that are not compatible with the parameter types.
  • Using the wrong parameter order.
  • Forgetting required arguments.
  • Using default parameters incorrectly.
  • Confusing parameters with arguments.
  • Changing original variables unintentionally when using references.
  • Forgetting to use const when a reference should not modify the object.
int add(int a, int b) {

    return a + b;
}

int result = add(10, 20);

27. Practical Calculator with Parameters

#include <iostream>

int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int multiply(int a, int b) {
    return a * b;
}

int main() {

    int x, y;

    std::cout << "Enter first number: ";
    std::cin >> x;

    std::cout << "Enter second number: ";
    std::cin >> y;

    std::cout << "Addition = "
              << add(x, y)
              << std::endl;

    std::cout << "Subtraction = "
              << subtract(x, y)
              << std::endl;

    std::cout << "Multiplication = "
              << multiply(x, y);

    return 0;
}

The same input values are passed to three different functions.

28. Practical Student Information Function

#include <iostream>
#include <string>

void showStudent(
    std::string name,
    int age,
    double marks
) {

    std::cout << "Name: "
              << name
              << std::endl;

    std::cout << "Age: "
              << age
              << std::endl;

    std::cout << "Marks: "
              << marks
              << std::endl;
}

int main() {

    showStudent("Amit", 19, 86.5);

    return 0;
}

Multiple parameters allow one function to receive different pieces of related information.

29. Best Practices for Function Parameters

  • Use meaningful parameter names.
  • Choose appropriate parameter data types.
  • Keep the number of parameters reasonable.
  • Use default parameters when a sensible default exists.
  • Use references when the function needs to work with the original object.
  • Use const references when the function should not modify the object.
  • Keep parameter order logical and consistent.
  • Use functions with parameters to make code reusable.
double calculateTotal(double price, int quantity) {

    return price * quantity;

}

30. Function Parameters – Final Summary

Concept Meaning
Parameter Variable declared in the function definition.
Argument Actual value passed to the function.
Multiple Parameters A function can receive several values.
Default Parameter Provides a value when an argument is not supplied.
Pass by Value Function receives a copy of the argument.
Pass by Reference Function can work with the original variable.
const Reference Allows efficient reference passing without modification through the reference.
returnType functionName(type parameter1, type parameter2) {

    // statements

}

📌 Key Points

  • Parameters allow functions to receive data.
  • An argument is the actual value passed to a parameter.
  • A function can have one or multiple parameters.
  • Parameters can have different data types.
  • Default parameters provide default values for omitted arguments.
  • By default, parameters are passed by value.
  • Reference parameters can modify the original variable.
  • const references can be used when modification is not required.
  • Parameters make functions reusable and flexible.
  • Choose meaningful names and appropriate types for parameters.

🧠 Quick Quiz

Question: What is a parameter in a C++ function?