🌟 Join our Telegram group for exclusive updates! Join Now Get Involved

Functions

C++ Functions

C++ Functions

Functions in C++ are blocks of code that perform a specific task and are defined with a name. They help in organizing code into modular and reusable units, improving readability and maintainability.

Function Declaration and Definition:


// Function declaration (prototype)
int add(int a, int b);

int main() {
    // Function call
    int result = add(3, 4);

    // Display the result
    std::cout << "Sum: " << result << std::endl;

    return 0;
}

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

In this example, the function add is declared at the beginning of the program (function prototype) and defined later. The main function calls the add function, passing two arguments (3 and 4), and stores the result in the variable result.

Function Parameters:


#include <iostream>

// Function declaration
void greet(std::string name);

int main() {
    // Function call with argument
    greet("Alice");
    greet("Bob");

    return 0;
}

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

In this example, the greet function takes a std::string parameter name, and the main function calls it with different arguments.

Return Values:


#include <iostream>

// Function declaration
int square(int x);

int main() {
    // Function call with argument
    int result = square(5);

    // Display the result
    std::cout << "Square: " << result << std::endl;

    return 0;
}

// Function definition
int square(int x) {
    return x * x;
}

In this example, the square function takes an integer parameter and returns the square of that parameter.

Default Values and Function Overloading:


#include <iostream>

// Function declaration with default value
int multiply(int a, int b = 2);

int main() {
    // Function calls
    int result1 = multiply(3);     // b takes the default value (2)
    int result2 = multiply(3, 4);  // b is provided as 4

    // Display the results
    std::cout << "Result 1: " << result1 << std::endl;
    std::cout << "Result 2: " << result2 << std::endl;

    return 0;
}

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

Recursive Functions:


#include <iostream>

// Recursive function to calculate factorial
unsigned long long factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    } else {
        return n * factorial(n - 1);
    }
}

int main() {
    // Function call
    int result = factorial(5);

    // Display the result
    std::cout << "Factorial: " << result << std::endl;

    return 0;
}

In this example, the factorial function is defined recursively to calculate the factorial of a number.

Functions play a crucial role in structuring C++ programs. They allow for code reuse, modularization, and the creation of readable and maintainable code. Understanding how to declare, define, and use functions is fundamental to writing effective C++ programs.

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing BYTEFOXD9, you agreed to use cookies in agreement with the BYTEFOXD9's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.