Functions in C++ – Complete Tutorial
Last Updated on: 9th Dec 2025 16:04:23 PM
1. Introduction to Functions in C++
In programming, many tasks are repeated again and again.
If we write the same code multiple times, the program becomes:
-
Long
-
Hard to read
-
Difficult to maintain
To solve this problem, C++ provides Functions.
A function is a block of code designed to perform a specific task, which can be called whenever needed. Functions help in code reusability, modularity, and clean program structure.
Every C++ program starts execution from the main() function, but large programs consist of many user-defined functions.
What is a Function?
A function in C++ is a self-contained block of statements that performs a specific operation, takes input as parameters (optional), processes the data, and may return a result.
Functions help to:
-
Break complex problems into smaller parts
-
Reduce code duplication
-
Improve readability
-
Enhance testing and debugging
Why Use Functions?
✔ Code reusability
✔ Easy debugging
✔ Shorter and cleaner code
✔ Modular program structure
✔ Team collaboration becomes easier
Types of Functions in C++
C++ provides two main types of functions:
-
Built-in (Library) Functions
-
User-defined Functions
1. Built-in Functions
Predefined functions provided by C++ libraries.
Examples
sqrt(), pow(), strlen(), cout, cin
Example Program
#include <iostream>
#include <cmath>
using namespace std;
int main() {
cout << sqrt(25);
return 0;
}
2. User-Defined Functions
Functions created by the programmer to perform customized tasks.
Structure of a Function
return_type function_name(parameters) {
// function body
return value;
}
Example
int add(int a, int b) {
return a + b;
}
Components of a Function
Every function in C++ consists of:
-
Function Declaration (Prototype)
-
Function Definition
-
Function Call
Function Declaration (Prototype)
Tells the compiler about the function before it is used.
Syntax
return_type function_name(parameter_list);
Example
int add(int, int);
Function Definition
The function definition contains the actual body of the function, where the logic is written.
Syntax
return_type function_name(parameter_list)
{
// function body
return value;
}
Example
int add(int a, int b) {
return a + b;
}
Function Calling
A function call executes the function by passing required values (arguments).
Syntax
function_name(arguments);
Example
int result = add(10, 20);
Example: Complete Function Program
#include <iostream>
using namespace std;
int add(int, int);
int main() {
int sum = add(5, 7);
cout << "Sum = " << sum;
return 0;
}
int add(int a, int b) {
return a + b;
}
Output
Sum = 12
Types of User-Defined Functions (Based on Arguments & Return Value)
1. Function with No Arguments and No Return Value
Function takes no input and returns no output.
void greet() {
cout << "Hello User!";
}
2. Function with Arguments but No Return Value
void printSum(int a, int b) {
cout << a + b;
}
3. Function with Arguments and Return Value
int square(int n) {
return n * n;
}
4. Function with No Arguments but Return Value
int getYear() {
return 2025;
}
void Functions
A void function does not return any value.
void show()
{
cout << "Welcome";
}
10. Function Parameters vs Arguments
| Term | Meaning |
|---|---|
| Parameter | Variables in function definition |
| Argument | Values passed during function call |
Example
int add(int a, int b); // parameters
add(10, 20); // arguments
Call by Value & Call by Reference
11.Call by Value
A copy of the variable is passed to the function. Original value does not change.
Example
void increment(int x)
{
x++;
}
int main()
{
int a = 10;
increment(a);
cout << a; // Output: 10
}
12. Call by Reference
Address of the variable is passed. Original value changes.
Example
void increment(int &x)
{
x++;
}
int main()
{
int a = 10;
increment(a);
cout << a; // Output: 11
}
13. Default Arguments
Default values assigned to parameters.
void show(int x = 5)
{
cout << x;
}
show(); // 5
show(10); // 10
14. Inline Functions
Compiler replaces function call with function code to reduce execution time.
inline int square(int x)
{
return x * x;
}
15. Function Overloading
Multiple functions with same name but different parameters.
int add(int a, int b);
float add(float a, float b);
16. Recursive Functions
A function that calls itself to solve a problem.
Example: Factorial
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
17. Scope of Variables
-
Local Variable – declared inside function
-
Global Variable – declared outside all functions
Real-Life Examples of Functions
✅ ATM machine → withdraw() , checkBalance()
✅ E-Commerce → addToCart() , makePayment()
✅ Calculator → add() , subtract()
✅ Bank App → deposit() , transfer()
Best Practices
✔ Use meaningful function names
✔ Keep functions small
✔ Use comments
✔ Avoid global variables
✔ Use pass-by-reference when required
Practice Questions
-
Create a function to find the largest of three numbers
-
Function to check prime number
-
Recursive function for Fibonacci
-
Calculator using functions
-
Swap two numbers using call by reference
21. Conclusion
Functions are the backbone of structured and modular programming in C++.
Every professional C++ program heavily depends on functions.
✅ Mastering functions is essential for DSA, competitive programming, and real-world projects.
Keep practicing — you're doing amazing!
Happy Coding! ![]()