Function in C++

When our program become so bigger to handle or when some lines are continuously repeating for e.g. If we are designing any software, a same type of calculations is needed repeatedly and we are writing the same line of code multiple times which in turn makes our program bigger, complex and difficult to handle and for detecting the error. To solve all these problems or in other words to make the problem simple, the function is designed.


 


What is a Function?

The function is a group of lines or subprogram to perform a specific task. As earlier explained, when the same type of calculations are repeating continuously, we design a function and give a specific name to it. Whenever it is further required in the program, it will be called by that name and then the code inside that function execute. As we all know, we always write a program inside the main function.

Type of Function 

There are two type of functions found in c++

  • Built-In function
  • User-defined function

Built-in function:

As we are including header files at the initial such as math.h or conio.h in which some functions are already defined are known as library functions or built-in function. These functions can be invoked directly without writing any code.

Library functions, getch(), clrscr() are the examples of built-in function.

User-defined function

C++ permits users to define their own functions that are known as a user-defined function.

For e.g. Main function

When the program executes, it starts with the main function. The main function is a user-defined function; users are needed to code in it. The programs can be divided into subpart.

For e.g.

Main()
(
F1();
F2();
}
F1()
{
}
F2()
{
}

The program starts with the main function, then it will go to F1(), Since F1(), F2() are user-defined functions, it will understand that this function is called from another location. The control of execution will transfer to the exact location of the function and it executes and then again the control will be transferred to the F1(), now again the process will continue for F2() also and after execution of code under F2(), the control will go to exit through curly braces.

The process of defining a user-defined function

This involves three steps to make user-defined function i.e.

  • Function prototype
  • Function Definition
  • Function Call

Function prototype

In C++, when there you want to use the user-defined function, there is a need to give function prototype such as type, return values, function name, parameters etc before it usage.

Function prototype includes four types i.e.

  • Function type: This is also known as the return values may be int, float etc. It specifies the type of function when it is called in a program.
  • Function name
  • Specifies the particular name of the function
  • Parameter list (Type of the arguments or void)
  • semicolon

Syntax:

Type function-name(parameter list);

Example

int sub (int,int);

int sub(void);

float add(int,float);


Function Call

Syntax

Function-name(parameter list);

For e.g.

int sub(int,int); // function prototype

Sub(4,2); // function call

z=sub(x,y); // function call


Function Definition

Function definition can be divided into two parts i.e. function header and function body. The header is similar to function call instead of semicolon used after parameter list.

In function header, there are three parts i.e. type, name of the function and parameter. In function body, the local variables, statements or return statement can be used according to the requirement.

Syntax

Type function name (parameter list)

{

// body

}

inside body, local variable declaration, statements or return statements can be used.

Example

int sub (int,int)

z= sub(x,y);

int sub(int x,int y)// function definition

{

statements;

return();

}

Lets understand with a program of user defined function in c++

#include <iostream>
using namespace std;
void Y(int &P, int &Q)
{
    P = P + Q;
    Q = P- Q;
    P = P - Q;
}
int main()
{
    int p = 14, q = 15;
    Y(p,q);
    cout << p << ", " << q;
    return 0;
}

Result:

15, 14

Coding Tag

Coding Tag always happy to serve you in Information Technology (IT). Learn HTML, CSS, Javascript, PHP, Android, Digital Marketing, Latest IT News and many more.

Leave a Reply

avatar
  Subscribe  
Notify of