Functions are blocks of code designed to perform a particular task. They help in breaking down complex problems into smaller, manageable parts, making code reusable, organized, and easier to maintain.
A function in C++ has the following structure:
return_type function_name(parameter_list) { // function body: statements to execute return value; // (optional depending on return_type) }
void
if the function does not return anything.{ }
that defines what the function does.Before calling a function, it must be declared (prototyped) so the compiler knows about it. The declaration specifies the functionβs name, return type, and parameters without the body.
return_type function_name(parameter_list);
Example:
int add(int a, int b);
The function is defined later in the program.
To use a function, you call it by its name and pass arguments if required. The control transfers to the function, executes its code, and returns back.
int result = add(5, 3); // Calls the function add and stores the returned value in 'result'
A function can return a value using the return
statement.
int add(int a, int b) { int sum = a + b; return sum; }
Functions can perform tasks without returning any value by using the void
keyword.
void printMessage() { cout << "Hello from function!" << endl; }
Functions can take inputs called parameters. Parameters act like variables inside the function and receive values (arguments) when called.
&
symbol, passes the actual reference (address). Changes inside the function affect the original variable.// Pass by value void increment(int x) { x = x + 1; } // Pass by reference void incrementRef(int &x) { x = x + 1; }
C++ allows multiple functions with the same name but different parameter lists. This is called function overloading. The compiler chooses the correct function based on argument types or number.
int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; }
Declaring a function with the keyword inline
suggests to the compiler to insert the function code directly where it is called, reducing function call overhead.
inline int square(int x) { return x * x; }
A function that calls itself is called a recursive function. Useful for problems that can be broken down into similar sub-problems like factorial or Fibonacci.
int factorial(int n) { if (n == 0) return 1; // Base case else return n * factorial(n - 1); // Recursive call }
You can assign default values to function parameters. If the caller does not provide those arguments, default values are used.
void greet(string name = "Guest") { cout << "Hello, " << name << "!" << endl; }
#include <iostream> using namespace std; // Function prototype int multiply(int a, int b); int main() { int x = 5, y = 7; cout << "Multiplying " << x << " and " << y << " = " << multiply(x, y) << endl; return 0; } // Function definition int multiply(int a, int b) { return a * b; }
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!