What is a Function in C++? Explore Type of Function with Example

What is a Function in C++? Explore Type of Function with Example

27 Jul 2024
Beginner
2.12K Views
13 min read
Learn with an interactive course and practical hands-on labs

Free C++ Foundation Course: Learn C++ In 21 Days

C++ Functions: An Overview

Functions are a very important programming construct. Every programming language from C to Python employs the use of functions. Understanding the concept of functions means covering a big portion of programming. In this C++ tutorial, we will understand functions in C++ and its crucial aspects like function parameters, prototypes, etc. For more in-depth understanding, consider our C++ Certification Course.

What is a Function in C++?

A function in C++ is a block of code written by the programmer to perform a specific task. E.g. function to calculate the area of a square. A function executes only when it is called. It is an important aspect of code reusability and understandability. You can divide your program into as many functions as you want to solve a difficult task.

There are two types of functions in C++:

  1. Standard Library Functions: Inbuilt functions in C++
  2. User-defined Functions: Created by users
In this tutorial, we will look at User-defined functions in C++.

C++ User-defined Functions

In C++, a programmer can create any function by himself to perform any task e.g. function to calculate the area of a square. A user-defined function groups code to perform a specific task and that group of code is given a name (identifier). When a programmer invokes his created function in any part of the program, the body of that function executes.

There are some aspects of a user-defined function in C++. They are

  1. Function Declaration
  2. Function Call
  3. Function Definition
  4. Function Parameters
  5. Function Prototype
  6. return statement
We will look at each one of them below:

Read More - C++ Interview Interview Questions for Experienced

1.) Function Declaration in C++

Syntax

returnType functionName (parameter1, parameter2,...) {
 // function body 
}
  • returnType signifies the type of value returned by the function. e.g. int, void.
  • functionName specifies the name of the function. You must give a function name relevant to the task the function performs. It increases understandability. e.g. area()
  • The parentheses () after functionName contains parameters. They are like values given by the programmer to be used by the function to perform its task. The () can be empty i.e. no parameters.
  • function body in the {} contains the code to perform the given task.

Example of Function Declaration in C++

// function declaration
void welcomeFunction() {
 cout << "Welcome to ScholarHat";
}

2.) Function Call in C++

As mentioned above, a function executes only when it is called. When you create/declare a function, it gets saved for use in the future.

Syntax

functionName (parameter1, parameter2,...);

Example of Function Call in C++

We will call the welcomeFunction() created above in the main() function
int main() {
 
 // function call 
 welcomeFunction(); 

}
  • You can even call a function as many times as you want.

    Example of Multiple Function Calls in C++ Online Compiler

    
    #include <iostream>
    using namespace std;
    void welcomeFunction() {
     cout << "Welcome to ScholarHat";
    }
    int main() {
     
     // function call 
     welcomeFunction(); 
     welcomeFunction(); 
     welcomeFunction(); 
     return 0;
    } 

    Output

    Welcome to ScholarHat
    Welcome to ScholarHat
    Welcome to ScholarHat
    

3.) Function Definition in C++

The body of the function is known as the function definition.

Example of Function Definition in C++

// function declaration
void welcomeFunction() {
 cout << "Welcome to ScholarHat"; //function definition
}

4.) Function Parameters in C++

As said above, parameters are the values given by the programmer during function declaration to be used by the function to perform its task. They are variables of the function specified in parentheses ().

Syntax

returnType functionName (parameter1, parameter2,...) {
 // function body 
}

There can be as many parameters in the () as you want in the function declaration. They must be separated by commas ,. The values of the function parameters are passed during a function call.

  • The parameters in the function call are known as arguments or actual parameters.
  • The parameters in the function declaration are known as parameters or formal parameters

Example of Function Parameters in C++ Compiler


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

void welcomeFunction(string value) //formal parameter{
 cout << "Welcome \t to \t" << value << endl;
}

int main() {
 welcomeFunction("ScholarHat"); //actual parameter
 welcomeFunction("ScholarHat"); 
 welcomeFunction("ScholarHat"); 

 return 0;
}
 

In the above C++ code, the welcomeFunction() takes a string parameter. In the main() function, we have called the welcomeFunction() three times with the string parameter value or argumentScholarHat. Therefore the function executes three times.

Output

Welcome to ScholarHat
Welcome to ScholarHat
Welcome to ScholarHat

Remember: The arguments in the function call must always match with the corresponding parameters in the function declaration.

5.) Function Prototype in C++

We saw the function declaration and function definition above. Did you notice that the user-defined function is always defined above the main() function? In other words, the user-defined function is always defined above the function call. What if we create our function after the function call?

In C++, if a user-defined function, such as welcomeFunction() is declared after the function call, an error will occur. Therefore, we always have to declare the function in C++ above the function call. But, we can separate the function declaration and function definition in C++. Here comes the role of function prototype in C++.

Function Prototype in C++ is just the function declaration without the function definition above the function call.

Syntax

returnType functionName(dataType1, dataType2, ...);

Example of Function Prototype in C++


#include <iostream>
#include <string>
using namespace std;
void welcomeFunction(string); //function prototype

int main() {
 welcomeFunction("ScholarHat"); //function call above function declaration
 return 0;
}

void welcomeFunction(string value) { //function definition
 cout << "Welcome \t to \t" << value << endl;
}

Here, the function prototype, void welcomeFunction(string); informs the compiler about the function name, welcomeFunction(), and its parameter of type string, value. Therefore, we can call the welcomeFunction() from the main() function before its definition.

Output

Welcome to ScholarHat

6.) return statement in C++

You all might be familiar with the return statement in C++ as you have already read it in the goto and return statements in C++ tutorial.

We saw returnType in the function declaration syntax. We read that it signifies the type of the value returned by the function. In the above examples we have given the return type as void which means the function doesn't return any value.

If you want your function to return a value, you must mention the required returnType in the function declaration and use the return statement in the function definition. The return keyword denotes that the function has ended. Any code after return inside the function is not executed.

Example of return statement in C++ Online Editor


#include <iostream>
using namespace std;

int returnFunction(int a, int b) {
 return a + b;
}

int main() {
 cout << returnFunction(20, 40);
 return 0;
}

In the above C++ code, the returnFunction() adds the values of a and b and returns it to the main() function from where it is called. As the returnFunction() returns an integer value the returnType of the function is int.

Output

60
Summary
We saw in this blog post almost all the aspects of functions in C++ with examples. Now, you need to do hands-on to familiarize yourselves with such an important programming construct. For practicals and hands-on experience, you can consider our C++ Certification program.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 9th time in a row (2016-2024). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this