Functions in C++

A function is a reusable block of code that performs a specific task. Instead of writing the same code multiple times, it can be packaged into a function and called whenever needed. Functions make programs modular, readable, and easier to maintain.

Why Use Functions?

  • Reusability — Write once, use many times.
  • Readability — Code is organized and easier to understand.
  • Maintainability — Fix a bug in one place, not everywhere.
  • Abstraction — Hide complexity behind a clean interface.

Syntax of a Function

return_type function_name(parameters) {
    // function body
    return value;
}

Creating and Calling a Simple Function

#include <iostream>
using namespace std;

void greet() {
    cout << "Hello! Welcome to C++." << endl;
}

int main() {
    greet();   // calling the function
    greet();   // call it again
    return 0;
}

Output:

Hello! Welcome to C++.
Hello! Welcome to C++.

void means the function does not return any value.

Functions with Parameters

Functions can accept input values through parameters:

void printSquare(int n) {
    cout << "Square of " << n << " is " << n * n << endl;
}

int main() {
    printSquare(4);
    printSquare(7);
    return 0;
}

Output:

Square of 4 is 16
Square of 7 is 49

Functions with Return Values

A function can compute a result and return it to the caller:

int add(int a, int b) {
    return a + b;
}

int main() {
    int result = add(5, 3);
    cout << "Sum = " << result << endl;
    return 0;
}

Output:

Sum = 8

Function with Multiple Parameters

double calculateArea(double length, double width) {
    return length * width;
}

int main() {
    double area = calculateArea(6.5, 4.0);
    cout << "Area = " << area << " sq units" << endl;
    return 0;
}

Output:

Area = 26 sq units

Function Declaration (Prototype)

If a function is defined after main(), the compiler needs a declaration (prototype) before main() so it knows the function exists.

#include <iostream>
using namespace std;

int multiply(int a, int b);    // declaration/prototype

int main() {
    cout << multiply(4, 5) << endl;
    return 0;
}

int multiply(int a, int b) {   // definition after main
    return a * b;
}

Output:

20

Default Parameters

Parameters can have default values. If the caller does not supply a value, the default is used:

void displayMessage(string msg = "Hello!") {
    cout << msg << endl;
}

int main() {
    displayMessage();             // uses default
    displayMessage("Good Day!");  // overrides default
    return 0;
}

Output:

Hello!
Good Day!

Pass by Value vs Pass by Reference

Pass by Value — original is not changed:

void doubleIt(int x) {
    x = x * 2;
}

int main() {
    int num = 5;
    doubleIt(num);
    cout << num;  // Still 5 — original unchanged
}

Pass by Reference — original is changed:

void doubleIt(int &x) {
    x = x * 2;
}

int main() {
    int num = 5;
    doubleIt(num);
    cout << num;  // Now 10 — original changed
}

Recursive Functions

A function that calls itself is called recursive. It must have a base condition to stop, otherwise it runs forever.

int factorial(int n) {
    if (n == 0) return 1;          // base condition
    return n * factorial(n - 1);  // recursive call
}

int main() {
    cout << "5! = " << factorial(5) << endl;
    return 0;
}

Output:

5! = 120

Inline Functions

For very small functions, the inline keyword suggests the compiler to insert the function code directly at the call site (to avoid function call overhead):

inline int square(int x) {
    return x * x;
}

int main() {
    cout << square(6) << endl;  // Output: 36
}

Key Takeaways

  • Functions are reusable named blocks of code.
  • They can take inputs (parameters) and return a result.
  • Use void when the function returns nothing.
  • Pass by reference allows the function to modify the original variable.
  • Recursive functions call themselves and must have a base case.
  • Default parameters provide fallback values when arguments are not passed.

Leave a Comment

Your email address will not be published. Required fields are marked *