programmingc++functions

Functions in C++: Building Reusable Code

By Swann
Picture of the author
Published on
Functions in C++

Introduction

Functions in C++ provide a method for structuring programs in segments of code to perform individual tasks. In C++, a function is a group of statements that is given a name, and which can be called from some point of the program.

Declaring, Defining, and Calling Functions

  • Declaration: Specifies the function’s name, return type, and parameters.
  • Definition: Contains the block of statements that defines what the function does.
  • Calling: Invoking the function from another function.
// Declaration
void greet();

// Definition
void greet() {
    std::cout << "Hello, World!" << std::endl;
}

// Calling
int main() {
    greet();
    return 0;
}

Function Parameters and Return Type

Functions can accept parameters and return values.

// Function that accepts two integers and returns their sum
int add(int a, int b) {
    return a + b;
}

Function Overloading

C++ allows function overloading, where multiple definitions of a function can exist with different parameter types.

void display(int a) {
    std::cout << "Integer: " << a << std::endl;
}

void display(double a) {
    std::cout << "Double: " << a << std::endl;
}

Default Arguments

Functions can have default arguments, providing default values if values are not provided during function calls.

void display(int a = 10) {
    std::cout << "Number: " << a << std::endl;
}

Recursion

A function that calls itself is known as a recursive function.

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

Conclusion

Functions serve as the building blocks of C++ programs, facilitating the creation of modular and organized code. By understanding the various aspects and types of functions, developers can craft code that is not only efficient and effective but also readable and maintainable.


Additional Resources

Stay Tuned

Want to become a Next.js pro?
The best articles, links and news related to web development delivered once a week to your inbox.