Function

suggest change

Here's an example of a function:

#include <iostream>

// function declaration
int add2(int i);
// functin overloading: same name but different arguments
int add2(int i, int j);
// default arguments: i is optional, if not given will be 0
int add3(int i = 0);

// function defintion
int add2(int i)       // Data that is passed into (int i) will be referred to by the name i
{                     // while in the function's curly brackets or "scope."
                    
    int j = i + 2;    // Definition of a variable j as the value of i+2.
    return j;         // Returning or, in essence, substitution of j for a function call to
                      // add2.
}

int add2(int i, int j)    // However, when add2() is called with two parameters, the
{                         // code from the initial declaration will be overloaded,
    int k = i + j + 2 ;   // and the code in this declaration will be evaluated
    return k;             // instead.
}

int add3(int i)
{
    return i + 3;
}

int main()
{
    std::cout << "add2(2)   : " << add2(2) << "\n";
    std::cout << "add2(3, 4): " << add2(3, 4) << "\n";
    std::cout << "add3()    : " << add3() << "\n";
    std::cout << "add3(3)   : " << add3(3) << "\n";
    return 0;  
}
add2(2)   : 4
add2(3, 4): 9
add3()    : 3
add3(3)   : 6

Function declaration

A function declaration declares a function but doesn't provide an implementation:

// declares a function add2 that takes a a single int argument named i
// and returns a result of type int 
int add2(int i);

In the example above, the int add2(int i) function declares the following to the compiler:

The argument name is optional; the declaration for the function could also be the following:

int add2(int); // in a declaration, name of the argument is optional

Per the one-definition rule, a function with a certain type signature can only be declared or defined once in an entire C++ code base visible to the C++ compiler. In other words, functions with a specific type signature cannot be re-defined – they must only be defined once. Thus, the following is not valid C++:

int add2(int i);  // The compiler will note that add2 is a function (int) -> int
int add2(int j);  // This is an error because add2 function (int) -> int has already been declared

If a function returns nothing, its return type is written as void. If it takes no parameters, the parameter list should be empty.

void do_something(); // The function takes no parameters, and doesn't return anything.

Function definition

A function definition defines the body of the function:

int add2(int i)
{
		// define variable j whose value is i + 2
    int j = i + 2;
		// return variable j as a result of function add2
    return j;
}

If a function has not been defined, definition is also a declaration.

Function call

A function can be called after it has been declared. For example, the following program calls add2 with the value of 2 within the function of main:

#include <iostream>

// declaration of function add2
// definition is in a different fle
int add2(int i);

int main()
{
		// call function add2() with argument 2 and print the result to standard output
    std::cout << add2(2) << "\n";
		return 0;  
}

Function Overloading

You can create multiple functions with the same name but different parameters.

int add2(int i)           // Code contained in this definition will be evaluated
{                         // when add2() is called with one parameter.
    int j = i + 2;
    return j;
}

int add2(int i, int j)    // However, when add2() is called with two parameters, the
{                         // code from the initial declaration will be overloaded,
    int k = i + j + 2 ;   // and the code in this declaration will be evaluated
    return k;             // instead.
}

Both functions are called by the same name add2, but the actual function that is called depends directly on the amount and type of the parameters in the call. In most cases, the C++ compiler can compute which function to call. In some cases, the type must be explicitly stated.

Default Parameters

Default values for function parameters can only be specified in function declarations.

int multiply(int a, int b = 7); // b has default value of 7.
int multiply(int a, int b)
{
    return a * b;               // If multiply() is called with one parameter, the
}                               // value will be multiplied by the default, 7.

In this example, multiply() can be called with one or two parameters. If only one parameter is given, b will have default value of 7. Default arguments must be placed in the latter arguments of the function. For example:

int multiply(int a = 10, int b = 20); // This is legal 
int multiply(int a = 10, int b);      // This is illegal since int a is in the former

Special Function Calls - Operators

There exist special function calls in C++ which have different syntax than name_of_function(value1, value2, value3). The most common example is that of operators.

Certain special character sequences that will be reduced to function calls by the compiler, such as !, +, -, *, %, and << and many more. These special characters are normally associated with non-programming usage or are used for aesthetics (e.g. the + character is commonly recognized as the addition symbol both within C++ programming as well as in elementary math).

C++ handles these character sequences with a special syntax; but, in essence, each occurrence of an operator is reduced to a function call. For example, the following C++ expression:

3+3

is equivalent to the following function call:

operator+(3, 3)

All operator function names start with operator.

While in C++’s immediate predecessor, C, operator function names cannot be assigned different meanings by providing additional definitions with different type signatures, in C++, this is valid. “Hiding” additional function definitions under one unique function name is referred to as operator overloading in C++, and is a relatively common, but not universal, convention in C++.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents