Functions in C++ - GeeksforGeeks
Functions in C++ - GeeksforGeeks
Search...
Functions in C++
Last Updated : 18 Mar, 2025
Function Definition
A function definition specifies the function name, what type of value it returns
and what it does. In C++, a function must be defined before it its used.
return_type name() {
// Function body
}
Example:
The above function is named printHello. Only the single statement that prints
“Hello Geeks” is the part of function body. The return type of the function is
void, which is used when function does not return anything.
Function Call
Once the function is defined, we can use it anywhere in the program simply by
calling it using its name with () parenthesis.
https://www.geeksforgeeks.org/functions-in-cpp/ 1/10
4/24/25, 12:27 PM Functions in C++ | GeeksforGeeks
Example:
▸ {...}
// Calling function fun
printHello();
▸ {...}
Output
Hello Geeks
The function printHello is called using its name in the main along with
parenthesis. When called, printHello’s body is executed and the text “Hello
Geeks” is printed.
Return Type
We have seen an example of function that does not return anything. But
functions can return some value as a result of its execution. The type of this
value should be defined as the return_type of the function.
Example:
▸ {...}
// Defining function that returns 10
int getTen(){
int res = 10
return res;
}
int main() {
▸ {...} Sign In
C++ Data Types C++ Input/Output C++ Arrays C++ Pointers C++ OOPs C++ STL C++ In
Output
10
In this example, the getTen() function is supposed to return the integer value.
So, the return type of the function is specified as int. Then the return keyword
is used to return the variable res from the function to the point where it is
https://www.geeksforgeeks.org/functions-in-cpp/ 2/10
4/24/25, 12:27 PM Functions in C++ | GeeksforGeeks
called. The call of the function is replaced by the value it returns, so res,
whose value is 10 is printed.
Note: Only one value can be returned from the function, and it must be of
the same type as of function’s return type.
Parameters or Arguments
A function can also take some input data to process. These values are called
function arguments and are supplied to the function at the function call. To
receive these values, we define placeholder variables called parameter inside
parenthesis in function definition. They should be specified with their types and
names.
name1, name2 and so on are the parameter names using which they will be
accessed in the function.
Example:
▸ {...}
// Defining function that prints given number
void printNum(int n){
cout << n << endl;
}
int main() {
int num1 = 10;
int num2 = 99;
▸ {...}
Output
10
99
https://www.geeksforgeeks.org/functions-in-cpp/ 3/10
4/24/25, 12:27 PM Functions in C++ | GeeksforGeeks
Here, there can be one question. Why didn’t we call the function with both
numbers at once? The answer lies in the printNum() definition. We have
defined the function that only takes one value of integer type as parameter, so
we cannot pass two values. In other words,
There are also other different ways to pass arguments to the function in C++.
Refer to this article to know more – Parameter Passing Techniques in C++
Forward Declaration
In C++, a function must be defined before its call. Otherwise, compiler will
throw an error. To resolve this, we can just declare the function before the call
and definition to inform the compiler about function’s name and return type.
This is called forward declaration or just function declarations.
return_type name();
Above is the function declaration that tells the compiler that there is a function
with the given name and return type in the program. Writing parameter in the
function declaration is optional but valid.
Library Functions
Until now, we have discussed function which we define by ourselves. These
functions are called user defined functions for obvious reasons.
C++ also provides some in-build functions that are already defined inside
different libraries to simplify some commonly performed tasks. These functions
are Library Functions which can be directly called to perform the task.
Example:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n = 3;
int m = 6;
▸ {...}
Output
The max() function in the above code returns the larger value among two
values passed as argument. It is defined in the algorithm library, so we had to
include the relevant header file <algorithm> to use it.
Similarly, C++ provides a lot of in-built functions to make our life easier. It is
preferred to use in-built functions wherever we can.
Default Arguments
https://www.geeksforgeeks.org/functions-in-cpp/ 5/10