


Khusboo Tayal
A function is a block of code that performs a specific task. You can call a function multiple times within your program, reducing code duplication and increasing code reusability.
A function in C++ has four key components:
return_type function_name(parameter_list) {
// Function body (statements)
}
#include <iostream>
using namespace std;
// Function definition
int add(int a, int b) {
return a + b;
}
int main() {
int result = add(5, 3); // Function call
cout << "Sum: " << result << endl;
return 0;
}
Sum: 8
A function must be declared before it is used. This can be done using a function prototype, which provides the function’s name, return type, and parameter list without the body.
return_type function_name(parameter_list);
#include <iostream>
using namespace std;
// Function Prototype
int multiply(int, int);
int main() {
int result = multiply(4, 5);
cout << "Product: " << result << endl;
return 0;
}
// Function Definition
int multiply(int a, int b) {
return a * b;
}
Product: 20
This is where you write the actual logic of the function.
// Function definition
int subtract(int a, int b) {
return a - b;
}To use a function, you simply call it by its name followed by parentheses.
int main() {
int result = subtract(10, 5);
cout << "Difference: " << result << endl;
}Difference: 5
Functions can accept inputs in the form of parameters. There are two main types:
#include <iostream>
using namespace std;
void changeValue(int x) {
x = 100; // Only a copy is modified
}
int main() {
int num = 50;
changeValue(num);
cout << "Value of num: " << num << endl;
return 0;
}
Value of num: 50
void changeValue(int &x) {
x = 100; // Original value is modified
}#include <iostream>
using namespace std;
void changeValue(int &x) {
x = 100; // Directly modifies the original variable
}
int main() {
int num = 50;
changeValue(num);
cout << "Value of num: " << num << endl;
return 0;
}
Value of num: 100
Default arguments allow you to specify default values for parameters, which are used if no value is provided during the function call.
int multiply(int a, int b = 1) {
return a * b;
}#include <iostream>
using namespace std;
int multiply(int a, int b = 2) {
return a * b;
}
int main() {
cout << "Multiply (3, 4): " << multiply(3, 4) << endl;
cout << "Multiply (5): " << multiply(5) << endl;
return 0;
}
Multiply (3, 4): 12 Multiply (5): 10
Function overloading allows you to create multiple functions with the same name but different parameter lists.
#include <iostream>
using namespace std;
// Overloaded functions
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
int main() {
cout << "Sum (int): " << add(3, 4) << endl;
cout << "Sum (double): " << add(3.5, 4.2) << endl;
return 0;
}
Sum (int): 7 Sum (double): 7.7
Inline functions are small functions defined with the inline keyword, which are expanded at the point of call rather than invoking a function call.
inline int square(int x) {
return x * x;
}#include <iostream>
using namespace std;
inline int square(int x) {
return x * x;
}
int main() {
cout << "Square of 5: " << square(5) << endl;
return 0;
}
Square of 5: 25
Recursion is when a function calls itself.
#include <iostream>
using namespace std;
// Recursive function
int factorial(int n) {
if (n <= 1)
return 1;
else
return n * factorial(n - 1);
}
int main() {
cout << "Factorial of 5: " << factorial(5) << endl;
return 0;
}
Factorial of 5: 120
In this tutorial, you learned: