Call by Value, Ref, Add
Call by Value, Ref, Add
There are only three ways one can pass arguments to a function:
● Pass By Value
● Pass By Reference
● Pass By Address
● Actual Parameters (also known as arguments) are the values that are being passed to
the function.
● Formal Parameters are the variables that are defined in the function definition.
There are two ways to pass value or data to function in C language: call by value and call by
reference. Original value is not modified in call by value but it is modified in call by reference.
In call by value, value being passed to the function is locally stored by the function parameter in
stack memory location. If you change the value of function parameter, it is changed for the
current function only. It will not change the value of variable inside the caller method such as
main().
1. #include <iostream>
2. using namespace std;
3. void change(int data);
4. int main()
5. {
6. int data = 3;
7. change(data);
8. cout << "Value of the data is: " << data<< endl;
9. return 0;
10. }
11. void change(int data)
12. {
13. data = 5;
14. }
Output:
Here, address of the value is passed in the function, so actual and formal arguments share the
same address space. Hence, value changed inside the function, is reflected inside as well as
outside the function.
1. #include<iostream>
2. using namespace std;
3. void swap(int *x, int *y)
4. {
5. int swap;
6. swap=*x;
7. *x=*y;
8. *y=swap;
9. }
10. int main()
11. {
12. int x=500, y=100;
13. swap(&x, &y); // passing value to function
14. cout<<"Value of x is: "<<x<<endl;
15. cout<<"Value of y is: "<<y<<endl;
16. return 0;
17. }
Output:
1 A copy of value is passed to the function An address of value is passed to the function
2 Changes made inside the function is not Changes made inside the function is reflected
reflected on other functions outside the function also
3 Actual and formal arguments will be created in Actual and formal arguments will be created
different memory location in same memory location
Inside the main function, we have x and y variables and on the next line, we have called swap
function. Now it will not send a value, it will send the addresses of variables x and y to swap
function.
Then who can take addresses? We know well the variables who store addresses.
So, pointers variables are used for storing addresses. In the ‘swap’ function, we made ‘a’ and ‘b’
as pointers by prefix with ‘*’. Now the formal parameters are pointers and actual parameters
are addresses of variables.
Inside the swap function, if we want to swap the values of the variables then we have to write
‘*’ then _variable_name i.e. ‘*a’ that is dereferencing the pointers otherwise variable without
‘*’ will be the address, not the value. So, this is the code for the call-by address. This time
‘swap’ function will swap the values of ‘x’ and ‘y’ as it is called by address. This swap function
can access actual parameters and it can modify them.
C++ provides inline functions to reduce the function call overhead. An inline function is a
function that is expanded in line when it is called. When the inline function is called whole code
of the inline function gets inserted or substituted at the point of the inline function call. This
substitution is performed by the C++ compiler at compile time. An inline function may increase
efficiency if it is small.
Syntax:
// function code
}
Remember, inlining is only a request to the compiler, not a command. The compiler can ignore
the request for inlining.
The compiler may not perform inlining in such circumstances as:
1. If a function contains a loop. (for, while and do-while)
2. If a function contains static variables.
3. If a function is recursive.
4. If a function return type is other than void, and the return statement doesn’t exist in a
function body.
5. If a function contains a switch or goto statement.
Constant Argument
A constant argument is the one whose modification cannot take place by the function.
Furthermore, in order to make an argument constant to a function, the use of a keyword const
can take place like- int sum (const int a, const int b). Moreover, the qualifier const in the
function prototype tells the compiler that modification of the argument must not take place by
the function.
By using the keyword friend compiler knows the given function is a friend function.
For accessing the data, the declaration of a friend function should be done inside the body of a
class starting with the keyword friend.
1. #include <iostream>
2. using namespace std;
3. class Box
4. {
5. private:
6. int length;
7. public:
8. Box(): length(0) { }
9. friend int printLength(Box); //friend function
10. };
11. int printLength(Box b)
12. {
13. b.length += 10;
14. return b.length;
15. }
16. int main()
17. {
18. Box b;
19. cout<<"Length of box: "<< printLength(b)<<endl;
20. return 0;
21. }
Output:
Length of box: 10