C Programming Function Definitions

Last modified by Microchip on 2023/11/09 09:06

Syntax

Function Definition

Syntax Examples

Here are two versions of the same function; they both return the maximum value between x and y.

Example 1

1 int maximum(int x, int y)
2 {
3   int z;
4  
5   z = (x >= y) ? x : y;
6   return z;
7 }

This one is more useful for illustrating the possible components of a function. In this first one, we show how to declare a local variable (scope will be discussed shortly).

Example 2 - A more efficient version

1 int maximum(int x, int y)
2 {
3   return ((x >= y) ? x : y);
4 }

This one is more efficient since it doesn't require the local variable z. This second one uses six fewer bytes of program memory than the first one – data memory usage is the same. z is temporarily allocated on the stack so no extra RAM is required, but more program code is needed to manipulate z.

  • The first line of the function is known as the function header. The header contains all of the information about how a program can interface with a function. It lists any parameters that the function will accept, along with their respective data types. It also defines what type of data is returned by the function. In this case, the function will accept two parameters, both of which are integers. Note that we cannot declare the parameters as a list of variables after one data type, such as int x, y, which was perfectly legal to do when declaring ordinary variables. In the header, each parameter variable must be individually listed after its data type. This function also returns an integer value.
  • In the first function, we declare a local integer variable z to use within the scope of this function. z is only accessible within this function, as are x and y.
  • In both functions, we use the conditional operator ?: to determine which parameter has the largest value. The first function assigns the maximum value to z first, then it returns the value of z to the program that called the function. In the second version of this function, we bypass the need to use a local variable and simply return the value of the expression directly.

Back to Top

Return Data Type

Syntax

Return Data Type

A function's type must match the type of data in the return expression. A function may have multiple return statements, but only one will be executed and they must all be of the same type

Return Data Type Example

1 int bigger(int a, int b)
2 {
3     if (a > b)
4         return 1;
5     else
6         return 0;
7 }

While a function can only return one value via the return statement, you may have multiple return statements that are conditionally executed. In this example, only one of the two return statements will be executed. If a is greater than b, then the first return statement will return the value 1. If a is less than or equal to b, then the second return statement will return the value.

The function type is void if

  • The return statement has no expression
  • The return statement is not present at all

Example

1 void identifier(type1 arg1,,typen argn)
2 {
3   declarations
4   statements
5   return; //return; may be omitted if nothing is being returned
6 }

Back to Top

Parameters

A function's parameters are declared just like ordinary variables but in a comma-delimited list inside the parentheses.

The parameter names declared in the function header are local to the function, meaning that they are not valid anywhere outside of the function.

A parameter list may mix data types, for example:
int foo(int x, int y, int z)

Parameters of the same type must be declared separately – in other words:
int maximum ( int x, y) will not work
int maximum ( int x, int y) is correct

Syntax

Function Parameters

If no parameters are required, use the keyword void in place of the parameter list when defining the function.

Example

1 type identifier(void)
2 {
3     declarations
4     statements
5     return expression;
6 }

Back to Top

How to Call / Invoke a Function

Function Call Syntax

  • No parameters and no return value
    • foo();
  • No parameters, but with a return value
    • x = foo();
  • With parameters, but no return value
    • foo(a, b);
  • With parameters and a return value
    • x = foo(a, b);

Back to Top