Function Syntax

This page was translated by a robot.

In C and C++, a function is written programmatically by declaring a return type, a symbol, and a collection of parameters. By declaring the function, it can be called elsewhere using the function call operator .

float area(float r);

In order to program a function, it is {}formulated using program code within curly brackets.

float area(float r){
  return r*r;
}

Details

The syntax and semantics of functions are explicitly described here on this page. For a general overview of the topic of declaration and definition, please refer to the relevant page under concepts .

A function declaration ends with a semicolon ;. The function definition, i.e. the actual programming of the function, uses curly brackets instead of the semicolon {}.

In certain circumstances, a function declaration can also be referred to as a prototype .

The return type associated with the parameter list is called the function type or signature . A unique function type therefore always consists of the return type AND the parameter list. Further explanations can be found under the function pointers .

Return Type

A function's return type comes first in the function declaration and specifies the type of value returned by the function.

float area(float r);

If the function does not return a value, it is a so-called procedure . A procedure differs from a function only in that a function returns a value, but a procedure does not. The term procedure is therefore not used in C and C++ , but is specified as a function with the return voidtype, which is to be interpreted for the return value as: invalid, to be ignored .

void print_addition(int a, int b);

voidIf you try to use the value in the program anyway for a function with a return type, the compiler will throw an error like the following: void value not ignored as it ought to be . Attempting to return a value in such a function despite specifying that no value should be returned produces a warning like this: return with a value, in function returning void .

Symbol

The programmer can assign a symbol to a function, or in other words give it a name. In the function declaration, the name is given in front of the brackets of the parameter list:

float area(float r);

Parameter List

Each function can declare any number of parameters that store the arguments passed to a function call. The individual parameters are later available within the function as normal variables. Parameters are specified as a so-called parameter list in round brackets ()after the function name.

int   do_nothing1    ();
int   do_nothing2    (void);
float area           (float r);
void  print_addition (int a, int b);
int   printf         (const char *restrict format, ...);

Note that other sources refer to the parameter list as the argument list. Here on ManderC, the signature of a function is always referred to as a parameter list, since it is a matter of declaring the variables available within the function. For more information on the difference between parameters and arguments, and the different passing mechanisms, see the Arguments and Parameters page .

The individual parameters basically correspond to variable declarations, (with type and name) and are separated from each other by commas . Note that this is NOT the sequence operator , but a simple listing using a comma. A parameter list with at least one parameter can also be expanded using the ellipsis... , allowing any additional number of parameters to be passed.

On C++, if an empty parameter list is specified, it means that the function expects no parameters. In C, an empty parameter list means that it is not specified how many parameters are expected. A function that is explicitly not supposed to take any parameters can (in C and C++) be specified using the voidkeyword as the only parameter within the brackets.

In C, if the entire parameter list is empty (including no void) when the function is declared in C, the symbol is merely declared as a function, but it is not specified how many parameters it actually takes. The parameter list is only determined once the definition of the function has occurred. This can lead to errors if the programming is careless. In the following example, the function is declared without a parameter list and can therefore be called with any arguments as long as it has not yet been defined. This results in erroneous outputs. However, this no longer works in C++.






1234
and now...
8094
#include <stdio.h>

void printInt();

int main(){
  printInt(1234);
  printf("and now...\n");
  printInt();
  return 0;
}

void printInt(int x){
  printf("%d\n", x);
}

Old-Style Parameter

In addition to the parameter list declaration listed above, which is common today, there is also an old method, which is generally referred to as old-style and is hardly used anymore these days. In the old-style variant, the symbols inside the round brackets are declared without a type. This hides the types to be transferred from the outside, so the declaration cannot be used as a prototype .

The type assignment stands as an independent declaration after the round brackets or, if the type assignment is completely absent, is intassumed by default.











30
#include <stdio.h>

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

int main(){
  printf("%i\n", multiply(5, 6));
  return 0;
}

C compilers still allow this syntax today, since this syntax is still partially used, especially in system programming. However, C++ compilers no longer allow this style.

Funktion Definition

The signature, i.e. the return type, the name and the parameter list are also referred to colloquially as the function header, since they are written at the top of the definition. After the head comes the so-called body , the body of the function, i.e. the programming of the functionality. Once a function has a body, it is defined.

In contrast to the declaration, a body is attached to a definition of the function by formulating ;the body in curly brackets instead of the semicolon.{}

void printInt(int x){
  printf("%d\n", x);
}

The spelling of exactly where the curly brackets go varies by style. The styles are named after famous people and don't play the slightest role for the compiler. The author has become accustomed to the above spelling, but the following spelling is also common:

void printInt(int x)
{
  printf("%d\n", x);
}

The function is programmed inside the curly brackets. The curly brackets form the function scope , i.e. the definition area of ​​the function. Within this area are the variable definitions and the executable code of the function.

It should be noted that the C90 standard dictates that within a function block all variable definitions must come BEFORE executing operations. A detailed version can be found in the statement blocks . As of the C99 standard, this restriction has been lifted.

The variables of the parameter list are automatically added to the definition area of ​​the function by the compiler and are therefore available within the function. A function is terminated using the returnstatement . Consequently, from this point in time, the variables within the function (like the parameters) are no longer valid. More detailed information on this can be found in the call stack .

If a parameter is not used within the body of the function, modern compilers issue a warning like unused parameter , depending on the setting . To suppress such warnings, the parameter can simply be recast within the voidbody.

Terminology for Functions

There are many refined terms for functions, but they are rarely used in the C and C++ languages. The term procedure , for example, denotes a function WITHOUT a return value. In C and C++, a procedure declaration is exactly the same as a function declaration, except that it uses the keyword as the return voidtype.

The term handler is mainly used for function pointers when they are used for so-called callback functions . A handler is too good German a treatment . It is also used for exception handling using try-catch . The term routine is to be understood as any sequence of commands, which, however, has a self-contained character. The term is also often used for exception handling using the try–catch-Structure In contrast to the handler, a routine basically always designates an existing, fully programmed command sequence. The term handler is used with a symbolic character, because on the one hand it can be used for function pointers whose exact content does not necessarily have to be known, and on the other hand because the term is used as a general umbrella term in the sense of any treatment: handling.

Caution: In addition to the handler , there is also the handle, which basically has little to do with functions. A handle generally designates a pointer or a reference to an object. More specifically, the object (usually) denotes a system-provided structure that (usually) cannot be directly accessed by the programmer. Handle means too well German: The handle, the holder, the handle. With a handle you can grab an object without burning your fingers. Another commonly seen definition of the handle has also evolved: a pointer to a pointer. Using a pointer to a pointer, objects can be passed, changed, deleted or set across multiple function levels without the handle itself changing its value.

The term sub-routine is not used in C and C++ and is included here for the sake of completeness. A sub-routine is actually what is called a function in C and C++, but a sub-routine has the character of a helper function. In other programming languages ​​such as BASIC, subroutines have their own control structure (GOSUB), which executes a simple jump to the address of the subroutine without changing the context of the variable. After the sub-routine has been processed, a return is made to the calling function. Calling a sub-routine is, so to speak, a simplified function call.

In the olden days in C it was possible to define functions within functions. However, these so-called nested functions are no longer used in C today (except possibly in system programming) and are switched off by default in modern compilers. There are no nested functions in the C++ language. Accordingly, it will not be discussed further on this page.