0% found this document useful (0 votes)
28 views5 pages

Functions in C++ - GeeksforGeeks

The document provides an overview of functions in C++, detailing their definition, calling methods, return types, and the use of parameters. It explains how to declare and define functions, including examples of user-defined and library functions. Additionally, it discusses the concept of forward declaration and the importance of matching argument types with function definitions.

Uploaded by

Ibrahim Adama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views5 pages

Functions in C++ - GeeksforGeeks

The document provides an overview of functions in C++, detailing their definition, calling methods, return types, and the use of parameters. It explains how to declare and define functions, including examples of user-defined and library functions. Additionally, it discusses the concept of forward declaration and the importance of matching argument types with function definitions.

Uploaded by

Ibrahim Adama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

4/24/25, 12:27 PM Functions in C++ | GeeksforGeeks

Search...

Functions in C++
Last Updated : 18 Mar, 2025

A function is a building block of C++ programs that contains a set of


statements which are executed when the functions is called. It can take some
input data, performs the given task, and return some result. A function can be
called from anywhere in the program and any number of times increasing the
modularity and reusability.

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
}

return_type: Type of value the function returns.


name: Name assigned to the function.
Function body: Set of statements in curly brackets { } are executed when
the function is called.

Example:

// Defining function that prints hello


void printHello(){
cout << "Hello Geeks";
}

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() {

// Calling function getTen()


cout << getTen();

▸ {...} 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.

return_type name(type1 name1, type2 name2...) {


// Function body
return val;
}

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;

// Calling printNum and passing both


// num1 and num2 to it one by one
printNum(num1);

▸ {...}

Output

10
99

https://www.geeksforgeeks.org/functions-in-cpp/ 3/10
4/24/25, 12:27 PM Functions in C++ | GeeksforGeeks

In the above program, printNum() function is defined with one integer


parameter n that it prints. It means that it will take one value of integer type
while calling and print it. We called printNum() two times, one with num1 and
one with num2, and in each call, it prints the given number. Note that we refer
to the passed argument using the name n not num1 or num2 in the function. It
is due to the concept called scope of variables in the main and printNum()
function.

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,

A function can only take as many arguments as specified in the


function definition and it is compulsory to pass them while calling it.
Also, they should be of same type as in the function definition.

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.

return_type name(type1, type2 ...);

The above variation of the function declaration is also called function


prototype or function signature. If this declaration is present before the
function call, then we have the freedom to define the function anywhere in the
program.
https://www.geeksforgeeks.org/functions-in-cpp/ 4/10
4/24/25, 12:27 PM Functions in C++ | GeeksforGeeks

Note: Function declarations are compulsory, but the function definition


already contains the function declaration as a part of it, so it is not
explicitly needed when function is defined at the start.

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;

// Call library function max() that


// returns maximum value between two
// numbers
cout << max(3, 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy