FUNCTION PARAMETERS IN C++
This tutorial provides function parameters in c++, call by reference in c++, call by value and call by reference in c++, reference argument in c++ with clear examples.
PARAMETERS
- It is the essential part of function. However, it is optional but it makes C++ function more dynamic than simple function
- It is used to passing information or value to a function and then function calculate the value and then returns appropriate results
- Based on type of parameters, a function responses and returns value
- C++ supports two types of method parameters. They are:
- Call by value (Pass by value)
- Call by reference (Pass by reference)
1. CALL BY VALUE (PASS BY VALUE)
- In call by value, original data is not modified
- By default, function arguments are passed by value in C++
- The value of actual parameter is copied to formal parameter (Sending a variable to the function through value)
- If there are any changes in formal parameters that do not reflect the value of actual parameters. Because both parameters are allocated in different memory addresses.
Actual Parameter (Actual Arguments)
- A parameter in calling function (function calling) or function declaration is called actual parameter.
Formal Parameter (Dummy Arguments)
- A parameter called function (function definition) is called formal parameter or dummy parameter.
NOTE
- It creates separate memory locations for both actual and formal parameters.
I. EXAMPLE OF CALL BY VALUE
1. SOURCE CODE
#include <iostream>
using namespace std;
void change(int d)
{
d+=10;
cout<<“Inside the method\n”;
cout<<“Value of formal parameter : “<<d<<endl;
cout<<“————————————-\n”;
}
int main()
{
cout<<“————————————-\n”;
cout<<“\tCall by Value\n”;
cout<<“————————————-\n”;
int a=30;
cout<<“Before passing value to function\n”;
cout<<“Value of actual parameter : “<<a<<endl;
cout<<“————————————-\n”;
// call by value
change(a);
cout<<“After passing value to function\n”;
cout<<“Value of actual parameter : “<<a<<endl;
cout<<“————————————-\n”;
return 0;
}
2. OUTPUT
2. CALL BY REFERENCE
- Here, the address of the actual parameter is copied to formal parameter (Sending a variable to the function through address)
- The changes in the formal parameter should reflect the actual parameter. Because both are using same memory address space
- In c++, the “call by reference” is implemented with help of pointer and reference “&” modifier.
CONDITIONS
- Only variables are allowed
- Expressions, Constants are not allowed.
Example
- swap(& (a+b), x) // wrong, because of expression
- swap(&45); // wrong because of constant
POINTS
- Unlike call by value, it does not create a new storage location (memory address), instead it just shares the memory address of formal parameter (dummy argument)
IMPLEMENTATIONS
- In c++, the call by address is implemented using two ways. They are:
1. Through Pointers (Pointer variable)
2. Through Reference (Reference variable)
II. CALL BY ADDRESS USING POINTERS
1. SOURCE CODE
#include <iostream>
using namespace std;
void change(int *ptr)
{
*ptr+=10;
cout<<“Inside the method\n”;
cout<<“Value of formal parameter : “<<*ptr<<endl;
cout<<“————————————-\n”;
}
int main()
{
cout<<“————————————-\n”;
cout<<“\tCall by Reference\n”;
cout<<“————————————-\n”;
int a=25;
cout<<“Before passing value to function\n”;
cout<<“Value of actual parameter : “<<a<<endl;
cout<<“————————————-\n”;
// call by reference
change(&a);
cout<<“After passing value to function\n”;
cout<<“Value of actual parameter : “<<a<<endl;
cout<<“————————————-\n”;
return 0;
}
2. OUTPUT
EXPLANATION (REFERENCE PARAMETER)
REFERENCE VARIABLE
- Unlike c language, c++ provides new features called as reference variable
- Like pointer, it is used to access and manipulate memory
- It is an alias (alternate name to existing variable)
- Unlike a pointer, it must be initialized with the value / refer of some variable. Because it cannot be NULL.
- A reference variable is similar to a pointer. In many cases, this variable is used as an alternative to pointer.
Working Operation
- A reference works as a pointer.
- A reference is declared as an alias of a variable.
- It stores the address of the variable.
Syntax
Example
EXAMPLE OF REFERENCE VARIABLE
SOURCE CODE
#include<iostream>
using namespace std;
int main()
{
int f=95; // Normal local variable
int &r=f; // Reference variable
cout<<“\tReference Variable\n”;
cout<<“————————————–\n”;
cout<<“Variable =”<<f<<endl;
cout<<“Reference Variable =”<<r<<endl;
cout<<“————————————–\n”;
r=60;
cout<<“Variable =”<<f<<endl;
cout<<“Reference Variable =”<<r<<endl;
cout<<“————————————–\n”;
f=90;
cout<<“Variable =”<<f<<endl;
cout<<“Reference Variable =”<<r<<endl;
cout<<“————————————–\n”;
return 55;
}
OUTPUT
CALL BY ADDRESS USING REFERENCE VARIABLE
- Aim of this code is used to implement the concept of call by address using a reference variable.
SOURCE CODE
#include <iostream>
using namespace std;
void change(int &rf) // reference variable
{
rf=rf+15;
cout<<“Inside the method\n”;
cout<<“Value of formal parameter : “<<rf<<endl;
cout<<“——————————————\n”;
}
int main()
{
cout<<“—————————————\n”;
cout<<“\tCall by Reference-Via Reference\n”;
cout<<“—————————————\n”;
int a=25; // actual variable
cout<<“Before passing value to function\n”;
cout<<“Value of actual parameter : “<<a<<endl;
cout<<“—————————————\n”;
change(a);
cout<<“After passing value to function\n”;
cout<<“Value of actual parameter : “<<a<<endl;
cout<<“—————————————\n”;
return 0;
}
OUTPUT
DIFFERENCE BETWEEN CALL BY VALUE & CALL BY REFERENCE
S.N | CALL BY VALUE | CALL BY REFERENCE |
1. | Copy of the value is passed to the function (Arguments are passed via value) | Copy of the address is passed to function (Arguments are passed by address) |
2. | Original value is not modified | Original value is modified |
3. | The actual and formal parameters are using different memory locations | Actual and formal parameters are using the same memory location. |
More Tutorials
-
How to convert Word to Number in Bash Script
-
How to Count Word Frequency in Bash Script
-
Bash Script – How to convert Number to Words
-
Linux Commands – How to use cat, zcat, tac and lolcat commands in Linux