BCA / B.Tech 10 min read

Parameter and Argument Passing in C++

Parameter and Argument Passing in C++:


In the C++ programming language, functions are used to perform a specific task. When we call a function, we can pass some data as input to that function. This data is known as parameters and arguments.

What is a Parameter?
A parameter is the variable or variables used in the definition of a function, which act as input for the function.

What is an Argument?
An argument is the actual value that is passed for a parameter when a function is called.

Types of Parameter and Argument Passing in C++:
1. Pass by Value: A copy of the argument's value is passed to the function. Changes made to the parameter inside the function do not affect the original argument.
2. Pass by Reference: The actual reference of the argument is passed to the function. Changes made inside the function are also effective outside the function.
3. Pass by Pointer: The memory address of the argument is passed to the function. This method also allows the original value to be changed.

Advantages and Disadvantages of Parameter and Argument Passing:
Pass by Value: Safe because the original variable is not affected, but uses more memory and can be expensive for large variables.
Pass by Reference and Pointer: Saves memory as no copy is made and allows direct modification of the original data, but can be risky if the function makes unintended changes.