0% found this document useful (0 votes)
8 views7 pages

Call by Value, Ref, Add

Functions in programming

Uploaded by

ankitahota3264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Call by Value, Ref, Add

Functions in programming

Uploaded by

ankitahota3264
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

3 Ways of Passing Arguments in C++

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.

Call by value and call by reference in C++

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.

Call by value in C++


In call by value, original value is not modified.

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:

Value of the data is: 3

Call by reference in C++


In call by reference, original value is modified because we pass reference (address).

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:

Value of x is: 100


Value of y is: 500

Difference between call by value and call by reference in C++

No Call by value Call by reference


.

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

Parameter Pass by Address in C++ with Examples:


void swap(int *a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main(){
int x = 10, y = 20;
swap(&x, &y);
cout << x << ” ” << y;
}

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.

Inline Functions in C++

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:

inline return-type function-name(parameters)

// 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.

C++ Friend function


If a function is defined as a friend function in C++, then the protected and private data of a class
can be accessed using 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.

Declaration of friend function in C++


1. class class_name
2. {
3. friend data_type function_name(argument/s); // syntax of friend function.
4. };
In the above declaration, the friend function is preceded by the keyword friend. The function
can be defined anywhere in the program like a normal C++ function. The function definition
does not use either the keyword friend or scope resolution operator.

C++ friend function Example


Let's see the simple example of C++ friend function used to print the length of a box.

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

You might also like