Home > C++ > Functions in C++

Looking for how to write a function in C++ ? Well before going to that, you should know that What is a function ? Functions are defined as the a set or block of code that collectively perform a task. As we already know and if you don’t you know now that we have at one function in every c++ program which is called main function. This function is included in every c++ program as every c++ program starts its execution from this main function. We can have any number of functions in a single c++ program.

Functions are helpful in a way that whenever a particular task is to be repeated we just need to call a function rather than rewriting the whole block of code. A single program can be divided in to a number of functions, division depends on how you want to divide your code but logically division should be in a way that a function should perform a single task.

How to Write a Function in C++

C++ provides a proper mechanism to write and use functions. It involves three steps:

  • Declaration of Function
  • Calling the Function
  • Definition / Body of Function

Function Declaration: –

Function declaration tells compiler about a function name its return type and number of arguments to be passed/ accepted to/by the function. It is written in proper format as:

Return-type Function-Name(Argument/s);

E.g.

void test ();

This function declaration tells compiler that a function named test is to be defined in the program with void as its return type i.e. it will return nothing and it takes no arguments.

Note: – if we are passing any arguments to our function than we must write their data type in function declaration for each of the arguments separated by comma.
E.g.

int sum (int, int);

Above function declaration tells compiler about a function named sum with integer as its return type and it also tells that function will accept two parameters of integer type. We can also write the above function declaration as:

int sum (int a, int b); both these are declaration for same function.

Function Calling: –

Function calling is done inside main function or any other function. A function is called as:
Function-Name(Argument/s);

E.g.

For a function declaration void test ();
a function call will be test ();

Function Definition: –

This is done after main function and this is where complete functionality of a function is defined. i.e. a set of statements or a block of code that collectively perform task will be written here.
Its syntax is:

Return-Type Function-Name(Argument/s)
{
	// set of statements to perform a task.
}

E.g.

For a function declaration void test ();
its definition will be like:
void test()
{
	// block of code to perform a task.
}

E.g.

#include "stdafx.h"
#include"iostream"

using namespace std;

Void test();

int main()
{
     test();
     return 0;
}
void test() 
{
     cout << "This is our first user defined C++ function."<<endl;
}

Output: –
What is a function
Function with arguments:-

E.g.

#include "stdafx.h"
#include"iostream"

using namespace std;

int sum(int, int);
int main()
{
     int  x, y;
     cout << "Please Enter Value of X : ";
     cin >> x;
     cout << "Please Enter Value of Y : ";
     cin >> y;
     cout << "Sum of X & Y is : "<<sum(x,y)<<endl;
     return 0;
}
int sum(int a, int b) 
{
     int c = 0;
     c = a + b;
     return c;
}

Output: –
How to write a function in c++
Note: – If the return type of a function is other than void then it is necessary to call the function in one of the following ways:

    1. Assign its value to a variable with same data type as return type of function.

E.g.
For function of above example, it should be like:

z= sum(x,y);
    1. Call function in cout statement.
cout<<sum(x,y);

Function with Default Arguments: –

These are the functions in which function will accept a number of arguments. And if the values for all the arguments are not passed while calling the function, then the function can use default values for those arguments where required.
Note: – It is necessary to keep default arguments at the end of argument list while declaring the function.
Below example will clarify this concept.

#include "stdafx.h"
#include"iostream"

using namespace std;

int sum(int a, int b = 10);
int main()
{
     int  x;
     cout << "Please Enter Value of X : ";
     cin >> x;
     cout << "Function will defult value for second argument."<<endl;
     cout << "Sum of X with default value of argument is : " << sum(x) << endl;
     return 0;
}
int sum(int a, int b)
{
     int c = 0;
     c = a + b;
     return c;
}

Output: –
functions with default arguments
But if we keep default value argument to start of argument list we will have an error at compile time.
default argument error

3 thoughts on “Functions in C++

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.