Functions in C++
Imagine you have written the same group of statements several times in a program. Every time you need that task, you would have to write the same code again. This makes the program longer and harder to manage.
Functions solve this problem. A function is a block of code written to perform a particular task. You write it once and can call it whenever you need it.
In this tutorial, we will cover C++ Functions, function declaration, definition, calling, parameters, return values, default arguments, inline functions, function overloading, recursion, and storage classes.
Function Declaration
Before using a function, you can tell C++ about it using a function declaration. It tells the compiler the function’s name, return type, and the parameters it will accept.
Example
int add(int, int);
This tells C++ that there is a function named add that takes two integers and returns an integer.
The declaration is also called a function prototype.
Function Definition
The function definition contains the actual code that the function will execute.
int add(int a, int b)
{
return a + b;
}
Here, int is the return type, add is the function name, and a and b are parameters.
The code inside { } is the function body.
Calling a Function
Writing a function does not automatically execute it. You need to call the function when you want it to run.
int result = add(10, 20);
cout << result;
Output
30
When add(10, 20) is called, the values 10 and 20 are passed to the function, and the function returns their sum.
Function Parameters in C++
Parameters are the values that a function receives when it is called. They allow the same function to work with different values.
void greet(string name)
{
cout << "Hello " << name;
}
You can call it with different names:
greet("Aman");
greet("Riya");
The function remains the same, but the value passed to name changes.
Return Values
A function can send a value back to the place where it was called. This is called the return value.
int square(int n)
{
return n * n;
}
Now:
cout << square(5);
Output
25
The return statement sends the result back to the caller.
If a function does not need to return anything, you can use void.
void message()
{
cout << "Hello";
}
Default Arguments
Sometimes you want a parameter to have a value that is used when no value is provided. You can do this with a default argument.
void greet(string name = "Guest")
{
cout << "Hello " << name;
}
Now you can write:
greet();
Output
Hello Guest
You can also provide your own value:
greet("Aman");
Inline Functions
An inline function is a small function where you can request the compiler to replace the function call with the function’s code.
inline int square(int n)
{
return n * n;
}
Inline functions are generally suitable for small functions. The inline keyword is a request to the compiler, not a guarantee that the function will always be expanded inline.
Function Overloading in C++
Sometimes you want to use the same function name for similar tasks but with different types or numbers of parameters. This is called Function Overloading in C++.
int add(int a, int b)
{
return a + b;
}
double add(double a, double b)
{
return a + b;
}
Both functions are named add, but their parameter types are different. C++ chooses the appropriate function based on the values you provide.
Recursive Functions in C++
A function that calls itself is called a recursive function.
For example, here is a simple countdown:
void count(int n)
{
if (n == 0)
return;
cout << n << endl;
count(n - 1);
}
When count(3) is called, it prints 3, then calls itself with 2, then 1, and finally stops when n becomes 0.
The condition that stops the function is important. Without it, the function would keep calling itself.
Storage Classes
Storage classes provide information about how a variable is stored and how long it remains available.
Some storage class specifiers you may come across in C++ are static, extern, and thread_local. For example, a static local variable keeps its value between function calls.
void count()
{
static int n = 0;
n++;
cout << n << endl;
}
Calling count() three times prints:
1
2
3
The value of n is not created again from 0 on every call.
Frequently Asked Questions (FAQs)
Q1. What are functions in C++ used for?
C++ Functions are used to divide a program into smaller, reusable blocks of code. They make programs easier to organize, understand, test, and maintain.
Q2. What are parameters and return values in C++ functions?
Function Parameters in C++ allow values to be passed into a function, while a return value sends a result back to the part of the program that called the function.
Q3. What is function overloading in C++?
Function Overloading in C++ allows multiple functions to have the same name but different parameter lists. The compiler selects the appropriate function based on the arguments provided.
Q5. What is recursion in C++?
Recursive Functions in C++ are functions that call themselves to solve a problem by breaking it into smaller versions of the same problem. A base condition is required to stop the recursion.
Q6. What are default arguments in C++ functions?
Default arguments provide predefined values for function parameters. If a value is not supplied when the function is called, the default value is automatically used.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
