CodeToLive

Functions in C++

Functions in C++ are reusable blocks of code that perform a specific task. They can take parameters, return values, and help in organizing code into modular and manageable pieces.

Defining a Function

A function in C++ is defined using the following syntax:


return_type function_name(parameters) {
    // Function body
}
      

Example:


#include <iostream>

// Function Declaration
void greet(std::string name) {
    std::cout << "Hello, " << name << "!" << std::endl;
}

int main() {
    greet("Alice");
    return 0;
}
      

Returning Values

Functions can return values using the return keyword.


#include <iostream>

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

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

Function Overloading

C++ supports function overloading, where multiple functions can have the same name but different parameters.


#include <iostream>

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

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

int main() {
    std::cout << "Sum (int): " << add(5, 10) << std::endl;
    std::cout << "Sum (double): " << add(3.14, 2.71) << std::endl;
    return 0;
}
      

Default Arguments

Functions can have default arguments, which are used if the caller does not provide a value.


#include <iostream>

void printMessage(std::string message = "Hello, World!") {
    std::cout << message << std::endl;
}

int main() {
    printMessage();  // Output: Hello, World!
    printMessage("Custom Message");  // Output: Custom Message
    return 0;
}
      

Inline Functions

Inline functions are small functions that are expanded in place when called, reducing the overhead of function calls.


#include <iostream>

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

int main() {
    std::cout << "Square: " << square(5) << std::endl;  // Output: 25
    return 0;
}
      

Lambda Expressions

Lambda expressions allow you to define anonymous functions directly in your code.


#include <iostream>

int main() {
    auto greet = []() {
        std::cout << "Hello, World!" << std::endl;
    };

    greet();  // Output: Hello, World!
    return 0;
}
      

Recursion

A function can call itself, which is known as recursion. Recursion is useful for solving problems that can be broken down into smaller, similar subproblems.


#include <iostream>

int factorial(int n) {
    if (n == 0) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int result = factorial(5);
    std::cout << "Factorial: " << result << std::endl;  // Output: 120
    return 0;
}
      

Function Pointers

Function pointers allow you to store the address of a function and call it dynamically.


#include <iostream>

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

int subtract(int a, int b) {
    return a - b;
}

int main() {
    int (*operation)(int, int); // Function pointer declaration

    operation = add; // Point to the add function
    std::cout << "Sum: " << operation(5, 3) << std::endl;

    operation = subtract; // Point to the subtract function
    std::cout << "Difference: " << operation(5, 3) << std::endl;

    return 0;
}
      

Passing Arrays to Functions

Arrays can be passed to functions by passing a pointer to the first element of the array.


#include <iostream>

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        std::cout << arr[i] << " ";
    }
    std::cout << std::endl;
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    printArray(arr, 5);  // Output: 1 2 3 4 5
    return 0;
}
      
Next: Classes and Objects