Function

suggest change

Here's an example of a function:

#include <iostream>

// function declaration
int add2(int i);
// function 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 definition
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)    // When add2() is called with two arguments, this
{                         // overload is selected. The one-argument add2
    int k = i + j + 2 ;   // is unchanged.
    return k;
}

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 add2, which takes 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

A non-inline function must be defined only once in the program. It may be declared more than once, and the parameter names in those declarations need not match. An inline function may be defined in more than one translation unit, but every definition must be identical. The following declarations name the same function:

int add2(int i);  // declares add2
int add2(int j);  // OK: same function; the parameter name need not match

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;
}

A function 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 file
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)    // When add2() is called with two arguments, this
{                         // overload is selected. The one-argument add2
    int k = i + j + 2 ;   // is unchanged.
    return k;
}

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

A default argument is written on a declaration. The definition may be that declaration. Do not give the same parameter a default argument again in another declaration in the same scope.

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 the default value 7. A parameter with a default argument cannot be followed by a parameter without one. 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 character sequences, such as !, +, -, *, %, and <<, are operators. For a class type, the compiler can resolve an operator by calling a function whose name starts with operator. Built-in operators are not function calls: 3 + 3 on int does not call operator+(3, 3). The same spelling is used when that operator is overloaded:

operator+(3, 3) // a call of an overloaded operator+, not of the built-in +

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