Sei sulla pagina 1di 7

C++ Functions

The function in C++ language is also known as procedure or subroutine in other


programming languages.
To perform any task, we can create function. A function can be called many
times. It provides modularity and code reusability.

Advantage of functions in C++


There are many advantages of functions.
1) Code Reusability
By creating functions in C++, you can call it many times. So we don't need to
write the same code again and again.
2) Code optimization
It makes the code optimized, we don't need to write much code.
Suppose, you have to check 3 numbers (531, 883 and 781) whether it is prime
number or not. Without using function, you need to write the prime number
logic 3 times. So, there is repetition of code.
But if you use functions, you need to write the logic only once and you can
reuse it several times.

Types of Functions
There are two types of functions in C programming:
1. Library Functions: are the functions which are declared in the C++ header
files such as ceil(x), cos(x), exp(x), etc.
2. User-defined functions: are the functions which are created by the C++
programmer, so that he/she can use it many times. It reduces complexity of a
big program and optimizes the code.
Declaration of a function
The syntax of creating function in C++ language is given below:
return_type function_name(data_type parameter...)
{
//code to be executed
}

C++ Function Example


Simple example of C++ function.
#include <iostream>
using namespace std;
void func() {
static int i=0; //static variable
int j=0; //local variable
i++;
j++;
cout<<"i=" << i<<" and j=" <<j<<endl;
}
int main()
{
func();
func();
func();
}
Output:
i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1

References in C++
References are like constant pointers that are automatically dereferenced. It is
a new name given to an existing storage. So when you are accessing the
reference, you are actually accessing that storage.
int main()
{ int y=10;
int &r = y; // r is a reference to int y
cout << r;
}
Output :
10
There is no need to use the * to dereference a reference variable.

int main()
{
int a = 10;// say address of 'a' is 2000;
int *p = &a; //it means 'p' is pointing[referencing] to 'a'. i.e p->2000
int c = *p; //*p means dereferencing. it will give the content of the address
pointed by 'p'. in this case 'p' is pointing to 2000[address of 'a' variable],
content of 2000 is 10. so *p will give 10.
}
Difference between Reference and Pointer
References Pointers

Reference must be initialized when it is Pointers can be initialized any time.


created.

Once initialized, we cannot reinitialize a Pointers can be reinitialized any number


reference. of time.

You can never have a NULL reference. Pointers can be NULL.

Reference is automatically dereferenced. * is used to dereference a pointer.

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().
include <iostream>
using namespace std;
void change(int data);
int main()
{
int data = 3;
change(data);
cout << "Value of the data is: " << data<< endl;
return 0;
}
void change(int data)
{
data = 5;
}
Output:
Value of the data is: 3

C++ Call by Reference


Another way of passing an argument to a function where where the actual value
of the argument is not passed. Instead, only the reference to that value is passed.
Example 1: Passing by reference without pointers
#include <iostream>
using namespace std;

// Function prototype
void swap(int&, int&);

int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

swap(a, b);

cout << "\nAfter swapping" << endl;


cout << "a = " << a << endl;
cout << "b = " << b << endl;

return 0;
}
void swap(int& n1, int& n2) {
int temp;
temp = n1;
n1 = n2;
n2 = temp;
}
Output

Before swapping
a=1
b=2

After swapping
a=2
b=1

In main(), two integer variables a and b are defined. And those integers are
passed to a function swap() by reference.
Compiler can identify this is pass by reference because function definition
is void swap(int& n1, int& n2) (notice the & sign after data type).
Only the reference (address) of the variables a and b are received in
the swap() function and swapping takes place in the original address of the
variables.
In the swap() function, n1 and n2 are formal arguments which are actually same
as variables a and b respectively.
There is another way of doing this same exact task using pointers.
Example 2: Passing by reference using pointers
#include <iostream>
using namespace std;

// Function prototype
void swap(int*, int*);

int main()
{
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;

swap(&a, &b);

cout << "\nAfter swapping" << endl;


cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}

void swap(int* n1, int* n2) {


int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;
}
The output of this example is same as before.
In this case, the address of variable is passed during function call rather than the
variable itself.

swap(&a, &b); // &a is address of a and &b is address of b

Since the address is passed instead of value, dereference operator must be used
to access the value stored in that address.

void swap(int* n1, int* n2) {


... .. ...
}

The *n1 and *n2 gives the value stored at address n1 and n2 respectively.
Since n1 contains the address of a, anything done to *n1 changes the value
of a in main() function as well. Similarly, b will have same value as *n2.

Potrebbero piacerti anche