vimwiki

C++ function

void functionName() {
    std::cout << "function are great";
}

Parameters and arguments

Parameters

Parameters are defined in the function declaration, inside the parentheses ().

Contain a name and datatype.

int feetToInches(int ft) {
    return ft * 12;
}

Argument

Value passed to the function when performing a function call.

test = feetToInches(6);

Default parameters

void message(std::string language = "C++") {
    std::cout << "I like" << language;
}

int main() {
    message();
    message"java";
}

Pass by reference

Allows a function to modify the value of its argument. It uses references.

Use the & operator.

void plusFive(int& i) {
    i += 5;
}

int main() {
    int j = 10;
    plusFive(j);
    // j == 15
}

Function overloading

Function overloading allows multiple functions to have the same name as long as they differ in their parameters.

It enables functions to handle different types/numbers of inputs.

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

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

To be overlaoded, functions should have at least one of the following properties: