Functions In C++ With Types & Examples

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

Types Of Functions In C++ Along With Their Uses.

In our earlier tutorials until now, we have seen the various concepts in C++ like variables, storage classes, operators, arrays, strings, etc.

In this tutorial, we will move ahead and discuss the concept of functions. Functions are also called methods, subroutines or procedures.

=> Read Through The Extensive C++ Training Tutorial Series Here.

FUNCTIONS IN C++

How Do We Define A Function?

A function is a set of statements that are put together to perform a specific task. It can be statements performing some repeated tasks or statements performing some specialty tasks like printing etc.

One use of having functions is to simplify the code by breaking it into smaller units called functions. Yet another idea behind using functions is that it saves us from writing the same code again and again. We just have to write one function and then call it as and when necessary without having to write the same set of statements again and again.

Types Of Functions In C++

In C++, we have two types of functions as shown below.

Types of functions in C++

Built-in Functions

Built-in functions are also called library functions. These are the functions that are provided by C++ and we need not write them ourselves. We can directly use these functions in our code.

These functions are placed in the header files of C++. For Example, <cmath>, <string> are the headers that have in-built math functions and string functions respectively.

Let us see an Example of using built-in functions in a program.

#include <iostream>
#include <string>
using namespace std;

int main()
{
string name;
cout << "Enter the input string:"; 
getline (std::cin, name);
cout << "String entered: " << name << "!\n"; 
int size = name.size();
cout<<"Size of string : "<<size<<endl;
  }

Output:

Enter the input string: Software Testing Help
String entered: Software Testing Help!
Size of string: 21

Here we are using the headers <iostream> and <string>. The data types and other input/output functions are defined in <iostream> library. String functions used like getline, size are a part of the <string> header.

User-Defined Functions

C++ also allows its users to define their own functions. These are the user-defined functions. We can define the functions anywhere in the program and then call these functions from any part of the code. Just like variables, it should be declared before using, functions also need to be declared before they are called.

Let us discuss user-defined functions in detail.

The general syntax for user-defined functions (or simply functions) is as given below:

return_type functionName(param1,param2,….param3)
 {
 Function body;
 }

So as shown above, each function has:

  • Return type: It is the value that the functions return to the calling function after performing a specific task.
  • functionName : Identifier used to name a function.
  • Parameter List: Denoted by param1, param2,…paramn in the above syntax. These are the arguments that are passed to the function when a function call is made. The parameter list is optional i.e. we can have functions that have no parameters.
  • Function body: A group of statements that carry out a specific task.

As already mentioned, we need to ‘declare’ a function before using it.

Function Declaration

A function declaration tells the compiler about the return type of function, the number of parameters used by the function and its data types. Including the names of the parameters in the function, the declaration is optional. The function declaration is also called as a function prototype.

We have given some examples of the function declaration below for your reference.

int sum(int, int);

Above declaration is of a function ‘sum’ that takes two integers as parameters and returns an integer value.

void swap(int, int);

This means that the swap function takes two parameters of type int and does not return any value and hence the return type is void.

void display();

The function display does not take any parameters and also does not return any type.

Function Definition

A function definition contains everything that a function declaration contains and additionally it also contains the body of the function enclosed in braces ({}).

In addition, it should also have named parameters. When the function is called, control of the program passes to the function definition so that the function code can be executed. When execution of the function is finished, the control passes back to the point where the function was called.

For the above declaration of swap function, the definition is as given below:

void swap(int a, int b){

             b = a + b;
             a = b - a;
             b = b - a;
}

Note that declaration and definition of a function can go together. If we define a function before referencing it then there is no need for a separate declaration.

Let us take a complete programming Example to demonstrate a function.

#include <iostream>
using namespace std;

void swap(int a, int b)  {           //here a and b are formal parameters
   b = a + b;
   a = b - a;
   b = b - a;
cout<<"\nAfter swapping: ";
cout<<"a = "<<a;
cout<<"\tb = "<<b;
return;
}
int main()
{
int a,b;
cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;

cout<<"a = "<<a;
cout<<"\tb = "<<b;
swap(a,b);           //here a and b are actual parameters
}

Output:

Enter the two numbers to be swapped: 5 3
a = 5 b = 3
After swapping: a = 3 b = 5

In the above example, we see that there is a function swap that takes two parameters of type int and returns nothing. Its return type is void. As we have defined this function before function main, which is a calling function, we have not declared it separately.

In the function main, we read two integers and then call the swap function by passing these two integers to it. In the swap function, the two integers are exchanged using a standard logic and the swapped values are printed.

Calling A Function

When we have a function in our program, then depending on the requirement we need to call or invoke this function. Only when the function is called or invoked, the function will execute its set of statements to provide the desired results.

The function can be called from anywhere in the program. It can be called from the main function or from any other function if the program is using more than one function. The function that calls another function is called the “Calling function”.

In the above example of swapping numbers, the swap function is called in the main function. Hence the main function becomes the calling function.

Formal And Actual Parameters

We have already seen that we can have parameters for the functions. The function parameters are provided in the function definition as a parameter list that follows the function name. When the function is called we have to pass the actual values of these parameters so that using these actual values the function can carry out its task.

The parameters that are defined in the function definition are called Formal Parameters. The parameters in the function call which are the actual values are called Actual Parameters.

In the above example of swapping numbers, we have written the comments for formal and actual parameters. In the calling function i.e. main, the value of two integers is read and passed to the swap function. These are the actual parameters.

We can see the definitions of these parameters in the first line of the function definition. These are the formal parameters.

Note that the type of formal and actual arguments should match. The order of formal and actual parameters should also match.

Return Values

Once the function performs its intended task, it should return the result to the calling function. For this, we need the return type of the function. The function can return a single value to the calling function. The return type of the function is declared along with the function prototype.

Let’s take an Example of adding two numbers to demonstrate the return types.

#include <iostream>
using namespace std;

int sum(int a, int b){
return (a+b);
}
int main()
{
int a, b, result;
cout<<"Enter the two numbers to be added: "; cin>>a>>b;

result = sum(a,b);

cout<<"\nSum of the two numbers : "<<result;
}

Output:

Enter the two numbers to be added: 11 11

Sum of the two numbers: 22

In the above example, we have a function sum that takes two integer parameters and returns an integer type. In the main function, we read two integers from the console input and pass it to the sum function. As the return type is an integer, we have a result variable on the LHS and RHS is a function call.

When a function is executed, the expression (a+b) returned by the function sum is assigned to the result variable. This shows how the return value of the function is used.

Void Functions

We have seen that the general syntax of function requires a return type to be defined. But if in case we have such a function that does not return any value, in that case, what do we specify as the return type? The answer is that we make use of valueless type “void” to indicate that the function does not return a value.

In such a case the function is called “void function” and its prototype will be like

void functionName(param1,param2,….param 3);

Note: It is considered as a good practice to include a statement “return;” at the end of the void function for clarity.

Passing Parameters To Functions

We have already seen the concept of actual and formal parameters. We also know that actual parameters pass values to a function which is received by the format parameters. This is called the passing of parameters.

In C++, we have certain ways to pass parameters as discussed below.

Pass by Value

In the program to swap two integers that we discussed earlier, we have seen that we just read integers ‘a’ and ‘b’ in main and passed them to the swap function. This is the pass by value technique.

In pass by value technique of parameter passing, the copies of values of actual parameters are passed to the formal parameters. Due to this, the actual and formal parameters are stored at different memory locations. Thus, changes made to formal parameters inside the function do not reflect outside the function.

We can understand this better by once again visiting the swapping of two numbers.

#include <iostream>
using namespace std;

void swap(int a, int b) { //here a and b are formal parameters
                b = a + b;
                a = b - a;
                b = b - a;
   
cout<<"\nAfter swapping inside Swap:\n ";  
cout<<"a = "<<a;
cout<<"\tb = "<<b;
return;
}
int main()
{
int a,b;
cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
    
cout<<"a = "<<a;
cout<<"\tb = "<<b;
swap(a,b);
cout<<"\nAfter swapping inside Main:\n ";   
cout<<"a = "<<a;   
cout<<"\tb = "<<b;
  }

Output:

Enter the two numbers to be swapped: 3 2
a = 3 b = 2
After swapping inside Swap:
a = 2 b = 3
After swapping inside Main:
a = 3 b = 2

We have simply modified the earlier program to print the values of formal parameters & actual parameters before and after the function call.

As seen from the output, we pass values a=3 and b=2 initially. These are the actual parameters. Then after swapping inside the swap function, we see that the values are actually swapped and a=2 and b=3.

However, after the function call to swap, in the main function, the values of a and b are still 3 and 2 respectively. This is because the actual parameters passed to function where it has a copy of the variables. Hence although the formal parameters were exchanged in the swap function they were not reflected back.

Though Pass by value technique is the most basic and widely used one, because of the above limitation, we can only use it in the cases where we do not require the function to change values in calling the function.

Pass by Reference

Pass by reference is yet another technique used by C++ to pass parameters to functions. In this technique, instead of passing copies of actual parameters, we pass references to actual parameters.

Note: References are nothing but aliases of variables or in simple words, it is another name that is given to a variable. Hence a variable and its reference share same memory location. We will learn references in detail in our subsequent tutorial.

In pass by reference technique, we use these references of actual parameters and as a result, the changes made to formal parameters in the function are reflected back to the calling function.

We modify our swap function for our readers to understand the concept better.

#include <iostream>
#include <string>
using namespace std;
 
void swap(int &a, int &b){
   int temp = a;
   a = b;
   b = temp;
 }
 
int main()
 {
   int a,b;
   cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
 
   cout<<"a = "<<a;
   cout<<"\tb = "<<b;
   swap(a,b);
   cout<<"\nAfter swapping inside Main:\n ";
   cout<<"a = "<<a;
   cout<<"\tb = "<<b;
 }
 

Output:

Enter the two numbers to be swapped: 25 50
a = 25 b = 50
After swapping inside Main:
a = 50 b = 25

Note: The pass by reference technique shown in the above example. We can see that the actual parameters are passed as it is. But we append an ‘&’ character to the formal parameters indicating that it’s a reference that we are using for this particular parameter.

Hence the changes made to the formal parameters in the swap function reflect in the main function and we get the swapped values.

Pass by Pointer

In C++, we can also pass parameters to function using pointer variables. The pass by pointer technique produces the same results as that of pass by reference. This means that both formal and actual parameters share the same memory locations and the changes made in function are reflected in the calling function.

The only difference that in a pass by reference we deal with references or aliases of parameters whereas in a pass by pointer technique we use pointer variables to pass the parameters.

Pointer variables differ with the references in which pointer variables point to a particular variable and unlike references, we can change the variable that it points to. We will explore the details of the pointer in our subsequent tutorials.

We present the swapping of two integers again to demonstrate the Pass by Pointer technique.

 
#include <iostream>
#include <string>
using namespace std;

void swap(int *a, int *b)
 {
   int temp = *a;
   *a = *b;
   *b = temp;
 }

int main()
 {
   int a,b;
   cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
   cout<<"a = "<<a;
   cout<<"\tb = "<<b;
   swap(a,b);
   cout<<"\nAfter swapping inside Main:\n ";
   cout<<"a = "<<a;
   cout<<"\tb = "<<b;
 }

Output:

Enter the two numbers to be swapped: 23 54
a = 23 b = 54
After swapping inside Main:
a = 54 b = 23

Thus as already said, there is no difference in the output of the program. The only difference is in the way in which the parameters are passed. We can notice that formal parameters are pointer variables here.

Default Parameters

In C++, we can provide default values for function parameters. In this case, when we invoke the function, we don’t specify parameters. Instead, the function takes the default parameters that are provided in the prototype.

The following Example demonstrates the use of Default Parameters.

#include <iostream>
#include <string>
using namespace std;
 
int mathoperation(int a, int b = 3, int c = 2){
 
  return ((a*b)/c);
 }
 
int main()
 {
  int a,b,c;
 
  cout<<"Enter values for a,b and c: "; cin>>a>>b>>c;
  cout<<endl;
  cout<<"Call to mathoperation with 1 arg : "<<mathoperation(a);
  cout<<endl;
  cout<<"Call to mathoperation with 2 arg : "<<mathoperation(a,b);
  cout<<endl;
  cout<<"Call to mathoperation with 3 arg : "<<mathoperation(a,b,c);
  cout<<endl;
 }
 

Output:

Enter values for a,b and c: 10 4 6

Call to mathoperation with 1 arg: 15
Call to mathoperation with 2 arg: 20
Call to mathoperation with 3 arg: 6

As shown in the code example, we have a function ‘mathoperation’ that takes three parameters out of which we have provided default values for two parameters. Then in the main function, we call this function three times with a different argument list.

The first call is with only one argument. In this case, the other two arguments will have default values. The next call is with two arguments. In this case, the third argument will have a default value. The third call is with three arguments. In this case, as we have provided all the three arguments, default values will be ignored.

Note that while providing default parameters, we always start from the right-most parameter. Also, we cannot skip a parameter in between and provide a default value for the next parameter.

Now let us move onto a few special function related concepts that are important from a programmer’s point of view.

Const Parameters

We can also pass constant parameters to functions using the ‘const’ keyword. When a parameter or reference is const, it cannot be changed inside the function.

Note that we cannot pass a const parameter to a non-const formal parameter. But we can pass const and non-const parameter to a const formal parameter.

Similarly, we can also have const return-type. In this case, also, the return type cannot be modified.

Let us see a code Example that uses const references.

#include <iostream>
#include <string>
using namespace std;

int addition(const int &a, const int &b){
   return (a+b);
 }
int main()
 {
   int a,b;
   cout<<"Enter the two numbers to be swapped: "; cin>>a>>b;
   cout<<"a = "<<a;
   cout<<"\tb = "<<b;
   int res = addition(a,b);
   cout<<"\nResult of addition: "<<res;
 }

Output:

Enter the two numbers to be swapped: 22 33
a = 2 b = 33
Result of addition: 55

In the above program, we have const formal parameters. Note that the actual parameters are ordinary non-const variables which we have successfully passed. As formal parameters are const, we cannot modify them inside the function. So we just perform the addition operation and return the value.

If we try to modify the values of a or b inside the function, then the compiler will issue an error.

Inline Functions

We know that in order to make a function call, internally it involves a compiler storing the state of the program on a stack before passing control to the function.

When the function returns, the compiler has to retrieve the program state back and continue from where it left. This poses an overhead. Hence, in C++ whenever we have a function consisting of few statements, there is a facility that allows it to expand inline. This is done by making a function inline.

So inline functions are the functions that are expanded at runtime, saving the efforts to call the function and do the stack modifications. But even if we make a function as inline, the compiler does not guarantee that it will be expanded at runtime. In other words, it’s completely dependent on the compiler to make the function inline or not.

Some compilers detect smaller functions and expand them inline even if they are not declared inline.

Following is an Example of an Inline Function.

inline int addition(const int &a,const int &b){
     return (a+b);
}

As shown above, we precede the function definition with a keyword “inline” in order to make a function inline.

Using Structs In Functions

We can pass structure variables as parameters to function in a similar way in which we pass ordinary variables as parameters.

This is shown in the following Example.

#include <iostream>
#include <string>
using namespace std;

struct PersonInfo
 {
   int age;
   char name[50];
   double salary;
 };

void printStructInfo(PersonInfo p)
 {
   cout<<"PersonInfo Structure:";
   cout<<"\nAge:"<<p.age;
   cout<<"\nName:"<<p.name;
   cout<<"\nSalary:"<<p.salary;
 }
int main()
 {
   PersonInfo p;
   cout << "Enter name: ";
   cin.get(p.name, 50);
   cout << "Enter age: "; cin >> p.age;
   cout << "Enter salary: "; cin >> p.salary;
   
   printStructInfo(p);
 }

Output:

Enter name: Vedang
Enter age: 22
Enter salary: 45000.00
PersonInfo Structure:
Age:22
Name: Vedang
Salary:45000

Using structs in functions - Output

As shown in the above program, we pass a structure to function in a similar manner as other variables. We read values for structure members from the standard input and then pass a structure to a function that displays the structure.

Conclusion

This was all about the basics of functions in C++.

We will explore more about the static functions in C++ in our upcoming tutorials.

=> Check Complete C++ FREE Training Series Here.

Was this helpful?

Thanks for your feedback!

Leave a Comment