Function in C++

Function in C++



What is Function ?

A function is a block of code that takes input, performs some specific tasks and produces output.

A function's name is that function's identifier so, it should be unique. We use a lot of functions in our regular code like main(), strcpy(), rand() and so on.

Why do we need Functions ? 

We write lines of statements or codes to perform some certain tasks. But sometime when we need to perform that task again and again, we write the code again and the code becomes too long and complicated. And for this reason, we use Function, where we write a set of statements once and use those codes again and again just by calling the function. 

Types of Functions :

There are two types of functions in C++  :-

1. Standard Library Function :
The standard library functions are built-in functions in C++ . These functions are declared in the C++ header files. Some header files are "stdio.h", "string.h", "stdlib.h" and so on.

Some library functions are main(), rand() etc.

Main Function :

In C++, main() function is special kind of function and every C++ program must have it. It serves as the entry point for the program. A compiler always starts running the code from the beginning of the main function.

Rand Function :

The rand() function is a standard library function. It is defined in the "iostream" header file. It is designed by the C++ developers to generate random numbers.

2. User-defined function :

A user can also create functions as per their need. Such functions created by the user are known as user-defined functions.

How to define a Function ?

Defining Function is a step-by-step procedure, like this :-

Step 1: Function Prototype 

At first, we need to create a Function Prototype. In simple word we have to declare the function that specifies function’s name, parameters and return type. A function prototype tells the compiler about a function name and how to call the function. Putting parameter names in function declaration is optional in the function declaration, but it is necessary to put them in the definition. A function prototype doesn’t contain function body but a function definition does.

Syntax : 

return_type function_name(type1 argument1, type2 argument2, ...);

Example :

int eruditors(int a, int b);
  
//name of the function is eruditors()
//return type of the function is int
//two arguments of type int are passed to the function

Step 2: Calling a Function
When a program calls a function, the called function performs its defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the value to the main program.

To call a function, we just need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.

Syntax : 

function_name(argument1, argument2, ...);

Example :

eruditors(x, y);

//here we are calling the eruditors() function and passing the value of variable x and y.

Step 3: Function Definition

A Function definition contains a set of statements to perform a specific task. When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes inside the body of a function.

Syntax : 

return_type function_name(type1 argument1, type2 argument2, ...)
{
	// body of function
}

Example :

int eruditors(int x, int y)
{
	// body of the function
}

In previous section we understood how to use a function. But one more thing we need to know that, instead of declaring Function Prototype and defining the Function twice we can define the function at top, before calling the function only once.

Here, in the following two examples we can see how we can use the function in different ways with and without defining the Function Prototype.

Function Program 1 :


#include <iostream>
using namespace std;

int eruditors(int, int);
//here we are declaring a function prototype eruditors() with a integer return type.

int main() 
{
	int a, b, result;
	cout << "Enter the first and second value : ";
	cin >> a >> b;
	result = eruditors(a, b);
	//here we are calling the eruditors() function and passing the value of variable a and b.
	cout << "The summation is : " << result;
	return 0;
}

int eruditors(int x, int y) 
{
	int z;
	z=x+y;
	return z;
	//here we are defining the eruditors() function as it will take the input of x and y as integer and add those values and store it in another variable z and at last it will return the value of z.
}

Function Program 2 :


#include <iostream>
using namespace std;

int eruditors(int x, int y) 
{
	int z;
	z=x+y;
	return z;
	//here we are defining the eruditors() function as it will take the input of x and y as integer and add those values and store it in another variable z and at last it will return the value of z
}

int main() 
{
	int a, b, result;
	cout << "Enter the first and second value : ";
	cin >> a >> b;
	result = eruditors(a, b);
	//here we are calling the eruditors() function and passing the value of variable a and b.
	cout << "The summation is : " << result;
	return 0;
}


Output :

Enter the first and second value : 47 91
The summation is : 138


Key Points about Function : 

  • ~ The execution of a C++ program begins from the main() function.
  • ~ Every function has a return type.
  • ~ A return statement can only return or pass a single value.
  • ~ An empty parameter list means that the parameter list is not specified and function can be called with any parameters.
  • ~ The parameter list must not differ in function calling and function declaration. We must pass the same number of functions as it is declared in the function declaration.

Advantages of a Function : 

  • ~ By using functions, we can avoid rewriting same logic/code again and again in a program.
  • ~ We can call a Function as many times we want and from any place in a program.
  • ~ We can use library functions without worrying about their internal working.
  • ~ The program will be easier to understand, maintain and debug when it is divided into multiple functions.

Disadvantages of a Function : 

  • ~ The complexity of the program increases.
  • ~ Function doesn't return more then one value at a time.
  • ~ Calling function is always slower than running the code inside the function itself.