Cpp Tutorial: C++ Functions

In this chapter we shall study about:
1. Function prototype declaration
2. Function Definition
3. Function call
4. Actual and formal arguments
5. Return statement.
6. Call by value
7. Call by address
8. Call by reference
9. Default arguments
10. Inline functions
11. Function Overloading
12. Recursion Function
While developing software applications, a larger program is divided into smaller modules called as functions.
Functions helps us to debug the program and maintain the program very easily. Functions can also help to reduce the size of the program.
Suppose if you need to calculate the sum of variables in two different locations, instead of writing the same summation code, it is better to split into a separate function. Thus reducing the program size.

1. Function prototype declaration

A function prototype is used to declare user defined functions. A function prototype will help the compiler to know about the function name, arguments it is accepting, and the return type.
So while defining a function later, if the programmer makes a mistake, compiler will show an error.
Below are the function prototype examples:
int sum (int num_1, int num_2);
void display (int arr[]);
A void function will not return anything.
Notice the semicolon at the end of the function prototype declaration.

2. Function Definition

After declaring a function prototype, we should write a set of statements that tells what the function should do. This is called as define a function. The set of statements written inside curly braces “{ }” is called as body of the function.
Example:
int sum (int num_1, int num_2)
{
	return (num_1 + num_2);
}

3. Function call

After a function is defined, a function should be called to invoke that function. A function should be called with it’s name and list of arguments. If the function returns a value, then it should be preceded with a variable.
Example:
Here we are calling “sum” function with 2 arguments. As “sum” function will return a value, it will be stored in “total_sum” variable.
int total_sum = sum (1, 3);

4. Actual and formal arguments

The arguments that are declared in calling function are called as actual arguments.
The arguments that are declared in the called function are called as formal arguments.
Example:
int main()
{
	sum (sub_1, sub_2); // actual arguments.
}
void sum (int num_1, int num_2) // formal arguments
{

}

5. Return statement.

Return statement is used to return a value to the calling function. When ever compiler will encounter a “return” statement, the control will transferred back to the calling function.
Syntax:
	return <variable>; or return;

6. Call by value:

Here, when a function call is made, the copy of actual arguments is passed to the formal arguments. Hence any changes made to the formal arguments inside a function will not effect the actual arguments of the calling function.
Example:
#include<iostream>
using namespace std;

void display(int a, int b);

int main()
{
	int a = 10;
	int b = 20;

	cout<<"Before calling display() value of a = "<<a <<" value of b = "<<b<<endl;

	display(a, b); // call by value or pass by value.

	cout<<"After calling display() value of a = "<<a <<" value of b = "<<b<<endl;
}

void display(int a, int b)
{
	a += 20;
	b += 40;

	cout<<"In display() value of a = "<<a <<" value of b = "<<b<<endl;

	return;

}
Output:
Before calling display() value of a = 10 value of b = 20
In display() value of a = 30 value of b = 60
After calling display() value of a = 10 value of b = 20

7. Call by address

In this case, the address of actual arguments is passed to the function using pointer. Here any change to the formal arguments will change the value of formal arguments.
Example:
#include<iostream>
using namespace std;

void display(int *a, int *b);

int main()
{
	int a = 10;
	int b = 20;

	cout<<"Before calling display() value of a = "<<a <<" value of b = "<<b<<endl;

	display(&a, &b); // call by address or pass by address.

	cout<<"After calling display() value of a = "<<a <<" value of b = "<<b<<endl;
}

void display(int *a, int *b)
{
	*a += 20;
	*b += 40;

	cout<<"In display() value of a = "<< *a <<" value of b = "<< *b<<endl;

	return;

}
Output:
Before calling display() value of a = 10 value of b = 20
In display() value of a = 30 value of b = 60
After calling display() value of a = 30 value of b = 60

8. Call by reference

We know that CPP allows us to assign a different name for an already defined variable, those variables are called as reference variables. Hence when we call a function with the help of reference variables, if you change the value of those variables in the called function then the actual value will also be changed.
Example:
#include<iostream>
using namespace std;

void display(int &a, int &b);

int main()
{
	int a = 10;
	int b = 20;
	int &alias_a = a;
	int &alias_b = b;
	cout<<"Before calling display() value of a = "<<a <<" value of b = "<<b<<endl;
	display(alias_a, alias_b); // call by reference or pass by reference.
	cout<<"After calling display() value of a = "<<a <<" value of b = "<<b<<endl;
}

void display(int &a, int &b)
{
	a += 20;
	b += 40;
	cout<<"In display() value of a = "<< a <<" value of b = "<< b<<endl;
	return;
}
Output:
Before calling display() value of a = 10 value of b = 20
In display() value of a = 30 value of b = 60
After calling display() value of a = 30 value of b = 60

9. Default arguments

C++ allows programmer to add default values to function arguments during function declaration. So when that function is called with less number of arguments, the default values are used.
This is useful when you update old function by adding new arguments, without breaking old functions that are calling with less arguments.
The default values should be started from right to left. Default values cannot be directly assigned to the arguments present in the middle.
Example:
#include<iostream>
using namespace std;

void display(int a, int b = 10);

int main()
{
	int a = 10;

	cout<<"Before calling display() value of a = "<<a <<endl;
	display(a); 

	return 0;	
}

void display(int a, int b )
{
	cout<<"In display() value of a = "<< a <<" value of b = "<< b<<endl;
	return;
}
Output:
Before calling display() value of a = 10
In display() value of a = 10 value of b = 10

10. Inline functions

When a function call is made, the control is passed from calling function to called function. While returning from the function, it is vice versa. If the programmer wishes to avoid it, then he should use “inline” functions.
When a function is declared as inline, the compiler will copy the function body and paste it where the function call is made. Thus avoiding the passing of control.
If the inline function is large, then compiler will treat it as a normal function.
Example:
#include<iostream>
using namespace std;

void display(int a, int b);

int main()
{
	int a = 10;
	int b = 30;

	display(a, b); 

	return 0;	
}

inline void display(int a, int b )
{
	cout<<"In display() value of a = "<< a <<" value of b = "<< b<<endl;
	return;
}
Output:
In display() value of a = 10 value of b = 30

11. Function Overloading

C++ allows the programmer to define function having same name, but the argument list should be different.
Example for function overloading:
#include<iostream>
using namespace std;

void sum(int a, int b);
void sum(float a, float b);


int main()
{
	int a = 10;
	int b = 30;

	float c = 34.56;
	float d = 45.67;

	sum(a, b); 
	sum(c, d); 


	return 0;	
}

void sum(int a, int b )
{
	cout<<"In sum of INT, the result is = "<<(a+b)<<endl;
	return;
}


void sum(float a, float b )
{
	cout<<"In sum of FLOAT, the result is = "<<(a+b)<<endl;
	return;
}
Output:
In sum of INT, the result is = 40
In sum of FLOAT, the result is = 80.23

12. Recursion Function

A function calling itself is called as recursion function. To stop the recursion, there should be a test case where the recursion stops.
In below example, we find out the factorial of a number using recursion:
#include<iostream>
using namespace std;

int factorial(int a);


int main()
{
	int a = 5;

	int result = factorial(a); 
	cout<<"The factorial of "<<a <<" is = "<<result<<endl;

	return 0;	
}


int factorial(int a)
{
	int result = 0;

	if (a == 1)
		return 1;
	else
		result = a * factorial(a - 1);

	return result;
}

Output:

The factorial of 5 is = 120
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *