info@tutsfinder.in



Functions


Function in C++ are same as function in C. Function is a block of code where we can implement our data to execute it. It is design to perform specific task. Functions have unique name so that other functions can call it or execute it.

Example-

void xyz(){
//code
}

Every program has at least one function, that is main() function. main() function tells the compiler that program execution will be start from here.

To define a function these parts play an role –

Return Type – Every function should have any return type either it is void or any other primitive data type.

Function Name – Function name must be unique. This is the name of the function that we have to execute.

Parameters – Parameters are optional, if you need to pass the parameter then you can. It declares the type and no. of arguments.

Define a function –

data_type function_name(arguments if any)
{
//code
}

Return values – void function does not return any value, if you wants to return value then you have to used any data type (int, string etc.) and use the return keywords inside the function.

For Example –

 int main(){
//code
return 0;
} 

There are two types of function –

1. Library functions

2. Used define functions

Library functions - These are pre-define functions or built in functions used in C++ programming.

We can used directly by invoking function directly.

For Example – toUpper(c), toLower(c), sqrt(x), isdigit(c), sin(x) , cos(x)

Users define functions – These functions can be created by users, these are user defined functions. It allows user to provide extra functionality in a program besides built functions.

Program to show user define functions –

#include <iostream>

using namespace std;

int area (int x);   //declaring function

int main()

{

 int a = 5;

 int b = area(a);   //calling function

 cout << b;

}

int area (int x)   //defining function

{

 return (x*x);

}


There are two ways to calling a function –

1. Call by Value

2. Call by Reference

1. Call by Value – In call by value the value is passed in function as an argument.

Both the actual and formal parameters refer to the same location. So, if you make any changes inside the function, then they are not reflected back in actual parameters.



#include <iostream>
using namespace std;
int add(int a,int b);
int main()
{
int a = 5;
int b = 10;
int c;
c=add(a,b);
cout << "Addition of sum is : " << c << endl;
return 0;
}
int add(int a, int b)
{
return a+b;
}


Output:

Addition of Sum is : 15


2. Call by reference – In call by reference we can pass the address of the variable as an argument.

This method copies the reference of actual parameters into the formal parameters. And both parameters stored in different memory location.



#include <iostream>
using namespace std;

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

int main()
{
    int a = 7, b = 8;
    cout << "a & b Before swapping" << endl;
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;

    swap(&a, &b);

    cout << "\na & b After 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:

a & b Before swapping
a = 7
b = 8

a & b After swapping
a = 8
b = 7