21
NovWhat is a Function in C++? Explore Type of Function with Example
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++:
- Standard Library Functions: Inbuilt functions in C++
- User-defined Functions: Created by users
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
- Function Declaration
- Function Call
- Function Definition
- Function Parameters
- Function Prototype
- return statement
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
()
afterfunctionName
containsparameters
. 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 thewelcomeFunction()
created above in the main()
functionint 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
oractual 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 themain()
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 thereturn
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