• Have Any Question?
  • contact@eitworld.com

cpp functions





C++ Functions

C++ Functions is the block of statements or building blocks that is used to perform the specific task and every program must have a function which main() function and program execution start from main() function. Functions in C++ provide the facility of write once and use any number of times for same task by calling the function and readability of the program, and provides code reusability. C++ Functions can be of two types built-in functions (provided by the system) and user defined functions (made by users).

At eitworld.com you can learn C++ Function with Examples in easy and simple and smart way.

  • In C++ programming, function is the block of statements.
  • Function is used to perform the specific task.
  • In C++ programming, functions are the building blocks that perform some specific task.
  • In C++ programming, every program must have a function which main() function.
  • In C++ programming, program execution starts from the main() function which is defined at the entry level of the program.
  • In C++ programming, we can use additional functions.
  • When we use the additional functions then first we have to declare those functions and we have to provide the functionality or working code or body to the functions.
  • After providing the body or working code or functionality, we can call those functions inside the main() function.
  • Inside a function, we can call same function. When we call the same function inside the function then it is called recursion.

Need Of Function

  • If we want to perform a task more than one time then there is no need to write the same code again and again. Then we will write that code once and can use any number of times in the program.
  • Functions provide the facility of write once and use any number of times for same task by calling the function.
  • Functions are used to divide the task into smaller module that maintains the readability of the program.
  • Function provides code reusability.
  • Function provides the facility of top-down modular programming.
  • Using function, the length of code can be small or reduced.
  • Using function, the debugging of code is easier.

function can have three parts:

  • Function declaration
  • Function definition
  • Function calling

Function declaration:

  • It is also called prototype declaration.
  • Function prototype defines the compiler about the function. In prototype declaration, we define three things:
    • Function name
    • Return type
    • Parameters or arguments list
  • The basic form of function declaration is as follows:

return_type function_name (parameter_list);

return_type: the return type defines the type of value that must be return by the function.
function_name: function_name must be unique in the programme and function_name is used to identify that function in the programme.
parameter_list: Function can have one or more arguments. Arguments are those values or variables that are passed when we call the function.

Here is an example of function declaration :

int add(int number1 , int number2);

In the above example, we have declared a function int add(int number1, int number2)
The return type of this function is int or integer and this function has two arguments
number1 and number2 of int type.

 

Function Definition

    • In function definition, we specify what the function will do actually.
    • In this case, we specify the set of statements that will be executed by this function.
    • The basic syntax of function definition is as follows:

return_type function_name( parameter_list )

{

body of the function

}

  • Here is an example of function:

int add(int number1, int number2)

{

return (number1 + number2);

}

In the above example, we have declared a function int add(int number1, int number2) The return type of this function is int or integer and this function has two arguments number1 and number2 of int type. This function will return the sum of the number means it will add the number1 and number2 and return to the function.

Function Calling:

    • After defining the statements, we can call this function whenever we required.
    • The basic form is as follows:
function_name (parameter_list);
  • We can understand the working of function with the help of following syntax: cpp-program

    main()

    {

    int number1=10,number2=10;

    add(number1 , number2); //calling function using actual arguments

    }

    void add(int num1 , int num2 ) // function definition using formal arguments

    {

    return(); // return value

    }

    • Actual arguments are those values or arguments which are passed when we are calling the function.
    • Formal arguments are those arguments or variables which are declared in the function definition.
    • When a function is called, control passes to the definition of called function and the working of calling function is stopped, and control will jump to the place where the function is defined, and when the execution of called function is accomplished then control returns back to calling function place and execute the next statement.
    • It is highly recommended that the number of actual and formal arguments must be same because when we call the function and pass the actual arguments to the function then these arguments are received by formal arguments of the called function. So the number of actual and formal arguments must be same.
    • Result is returned from the function to the calling place of the program using the return statement.
    • We can understand the working of function with the help of following syntax:

Here is an example that shows the concept of function:

Example

#include<iostream.h>

#include<conio.h>

void add(int number1, int number2);//function declaration

void main()

{

int num1=5,num2=6;

add(num1,num2);//function calling

getch();

}

void add((int number1, int number2) //function definition

{

int result = number1 + number2 ;

cout<<result;

}

Output

Addition:11

In the above example, we have declared a function void add(int number1, int number2).

This function has two arguments that are type of int.

Inside the main() function, we have declared two variable num1 and num2 of int type which are initialized with the integer value 5 and 6 respectively means num1 = 5 and num2 = 6.

When we will call the function add() then we have to provide two values or variables of int type.

Before calling the function, we must have to provide the functionality to function.

In this example, function add() has two parameters of int type. In this function, we used a variable result that holds the sum of the number1 and number2.

Passing Argument to Function :

In C programming, there are two ways to passing arguments to the function:

  • Call by value
  • Call by reference

Call by value :

  • In call by value, when we pass the value to the function is locally stored by the function parameter in stack memory location.
  • If we change the value of function parameter or pass the different value, it is changed for the current function only. It will not change the value of variable inside the caller function such as main() .
  • In call by value, original value cannot be changed or modified because copy of actual arguments is used.
  • Here is a program that shows the concept of Call by value:

#include<iostream.h>

#include<conio.h>

void add(int x,int y); //function declaration

int main()

{

int a=4,b=5;

add(a,b);//function calling

cout<<“nInside main a=”<<a<<“b=”<<b;

getch();

return 0;

}

void add(int a,int b) //function definition

{

a=10;

b=12;

cout<<“nInside function a=”<<a<<“b=”<<b;

}

In the above example of function, we have declared a function void add(int x , int y). This function has two parameters of int type.

Inside the main() function, we have declared two variables with initialization a=5 and b=6 of int type. After that we have called the add(a,b) function, when we will call the function then the function will take two integer type values or variable. Here when we call the function then we are passing a and b to the function as the actual arguments.

When we pass the actual arguments then the copy of values or variables are used so that original values remain unchanged. In the above function, when function execution will be started then a copy of variables value is passed , when control will transfer to the function then the function arguments will be initialized with the passed value.

If we want to assign the other values to the function argument then it is possible. Here in the above example, we have provided other values to the function arguments means a=10 and b=12.

In this example, when control will enter into the function, we used cout<<“nInside function a=”<<a<<“b=”<<b; to print the value of a and b. It will print Inside main a=10,b=12.

After complete execution of the function, control will be transfer to the calling place. After that we have used cout<<“nInside main a=”<<a<<“b=”<<b; to print the actual values of a and b. It will print Inside function a=4,b=5

Output

Inside function a=4,b=5

Inside main a=10,b=12

Call by reference

  • In call by reference, the address of value is passed.
  • In call by reference, address of the value is passed in the function, so both actual arguments and formal arguments share the same address.
  • Thus, value changed inside the function, is reflected both inside and outside the function.
  • The following example shows this concept:

#include<iostream.h>

#include<conio.h>

int main()

{

int a=6,b=7;

add(&a,&b); //function calling

cout<<“nInside main a=”<<*a<<“b=”<<*b;

getch();

return 0;

}

void add(int *a,int *b)//function definition

{

*a=3;

*b=4;

cout<<“nInside function a=”<<*a<<“b=”<<*b;

}

In the above example, we have declared two variables with initialization a=6 and b=7.

After that we are calling the add(&a,&b) function. In this function, we are passing the address or reference of the variables a and b. When control will transfer to the function for execution then we have reassigned the values for the variable. Now values of variables are changed for whole program because we have passed the address of the variables. So the final output is

Output

Inside function a=3,b=4

Inside main a=3,b=4

Recursion:

  • When a function calls itself then this process is called as recursion and the function is called recursive function.
  • When you are using recursion then always define the condition to come out the infinite loop.
  • The example of recursion is as follows:

#include<iostream.h>

#include<conio.h>

int fact(int number); //function declaration

int main()

{

int a,b;

cout<<“nEnter number:”;

cin>>a;

b=fact(a);//function calling

cout<<“nfactorial of number is:”<<b;

getch();

return 0;

}

int fact(int number) //function definition

{

int f = 1;

if (number == 1)

{

return 1;

}

else

{

f= number * fact(number – 1); //recursion

} return f;

}

In the above example, we have declared a fact(int number) to calculate the factorial of number.
In this program, we have declared two variables a for hold the user input value and b is used to hold the value which is returned by the function.
When function will call then a parameter will be passed to function. In the function, we have declared a variable f=1.
Now we are checking a condition to test if number is equal to 1 then it will return 1, if number is greater than 1 then else part will be executed. Inside else part, we have called the function again.
This function will execute till the number will not be equal to 1. When number will 1 then it will terminate and calculated values stored into the f variable as result. Then the function will return the factorial of given number and return to the calling place. At calling place, the value returned from the function will store in b variable.
The output of the above program is as follows:

Output

Enter number:5

factorial of number is:120



Real Time Web Analytics