C++ Functions


What are Functions in C++?

  • Functions are used for the structured programming which follows top-down approach.Function is a collection of statements or set of statements that perform the task together.
  • In C++, every program consists of a single function that is a main( ) function. The functions take the inputs and perform the task on those inputs and provide output.
  • It provides standard to a program and while creating a program we use functions which makes program easier to understand, edit and check the errors, etc.

There are two types of functions in C++:

1) Library Functions in C++:

These functions are the in-built functions provided to a programmer who writes C++ program as it saves the programmer’s time. For example: strcpy( ), strcat( ), sqrt( ), etc. Programmers can invoke the functions directly and they do not need to write it again.

2) User defined Functions in C++:

User defined functions are those functions which are defined by the user. These functions can be created by the user. It allows performing the additional functions besides the in-built functions. Some of the examples of user defied functions are: add( ), multiply( ), divide( ) etc.

Advantages of Functions:

Some of the advantages of using functions in C++ program are:

  • It makes the program clear and easy to understand.
  • Single functions can be tested easily for errors.
  • It saves time from typing the same functions again and again.
  • It helps to modify the program easily without changing the structure of a program.

How functions are declared in C++?

  • In C++, the function declaration tells the compiler about a function name and how to call the function.
  • While declaring the function it also tells about the number of parameters the function takes, data types of the parameters and the return type of the function.

Now, let us have a look at the example to understand the concept better:

int add(int, int);  //this function takes two parameters of interger data type and returns an integer

How to define functions in C++?

In the function definition, we provide the body of the function. The statements are mentioned in the function definition that task is performed on those statements.

Let us have a look at the example:


How to pass Parameters in Function?

There are two types of parameters “actual and formal parameters” that are passed in a function. The actual parameters are passed in a function like a = 20, b = 30 whereas the formal parameters are those parameters which are received by the functions like int m and int n.

You can pass the parameters in the two ways:

1) Pass by value:

In the pass by value, 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.

2) Pass by reference:

In the pass by reference, the values of actual parameters are copied to formal parameters and both the parameters are stored in different memory locations. So, if you make any changes in formal parameters, then they are reflected back in actual parameters.

Let us have a look at the examples of Pass by value and Pass by reference:

Pass by value:


Pass by reference:



Share this website !!!
Facebook Twitter Linkedin