Function Definitions

A function definition specifies the name of the function, the types and number of parameters it expects to receive, and its return type. A function definition also includes a function body with the declarations of its local variables, and the statements that determine what the function does.

Syntax

function-definition:  type-specifier declarator compound-statement

declarator: identifier parameter-list

The parameter list in a definition uses this syntax:

parameter-list: ( parameter-declaration-list opt )

parameter-declaration-list: parameter-declaration parameter-declaration , parameter-declaration-list

parameter-declaration: type-specifier identifier

The syntax for the function body is:

compound-statement:  { statement-listopt }

statement-list: statement statement statement-list

Example

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

This simple example defines the add function to have the int return type. The function takes two parameters of type int, called a and b. In the body of the function, it will return the addition of both variables.

Last updated