CPP Tutorial



C++ FUNCTIONS


Functions in C++

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.


1. Function Definition and Syntax

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)
}
  
  • return_type: The data type of the value the function returns. Use void if the function does not return anything.
  • function_name: The unique name to identify the function.
  • parameter_list: Comma-separated list of inputs (parameters) passed to the function. It can be empty if no parameters are needed.
  • function body: Code block enclosed in curly braces { } that defines what the function does.

2. Function Declaration (Prototype)

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.


3. Calling a Function

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'
  

4. Function with Return Type

A function can return a value using the return statement.

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

5. Void Functions (No Return Value)

Functions can perform tasks without returning any value by using the void keyword.

void printMessage() {
    cout << "Hello from function!" << endl;
}
  

6. Function Parameters

Functions can take inputs called parameters. Parameters act like variables inside the function and receive values (arguments) when called.

  • Pass by Value: Default behavior, copies the value of argument into the parameter. Changes inside function do not affect the original variable.
  • Pass by Reference: Using & 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;
}
  

7. Function Overloading

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;
}
  

8. Inline Functions

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;
}
  

9. Recursive Functions

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
}
  

10. Default Arguments

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;
}
  

11. Summary

  • Functions help modularize code and avoid repetition.
  • Defined with return type, name, parameters, and body.
  • Can return values or be void.
  • Support pass by value and pass by reference.
  • Allow function overloading and default arguments.
  • Can be recursive and inline for optimization.

Example Program Using Functions

#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;
}
  

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review