Storage Classes for Functions in C

In C language, the declarations and definitions of a function can also include storage classes, similarly to the case of variables.

However, unlike the case of variables, functions can only have the extern and static classes.

In this lesson we will study these two options for functions.

Functions declared as extern

A function in C language can be declared with the extern storage class. The syntax is as follows:

extern return_type function_name(parameters);

When we declare a function with extern we are saying that its linkage is external, in other words, that function can be called from other source files.

Declaring a function as extern is redundant since all functions are extern by default. However, it is possible to declare a function as extern to clarify that the function is defined in another source file.

Therefore, the following definitions are equivalent:

int sum(int a, int b);
extern int sum(int a, int b);

To recap:

Definition

Functions declared as extern

A function declared as extern has external linkage, that is, it can be called from other source files different from the one in which it is defined.

The syntax is as follows:

extern return_type function_name(parameters);

All functions are extern by default, so declaring a function as extern is redundant.

Functions declared as static

Declaring a function as static has a different meaning compared to the case of variables.

A function declared as static has internal linkage, that is, it can be called only within the source file in which it is defined. For example:

static int sum(int a, int b) {
    return a + b;
}

In this case, the sum function can be called only within the source file in which it is defined. If you try to call sum from another source file, the compiler will return an error.

Declaring a function as static does not completely prevent the possibility of invoking a function from another source file. It is always possible, in fact, to use a function pointer to do so.

In any case, the use of static for functions has a whole series of advantages:

  • Ease of Maintenance:

    When we declare a static function we have the guarantee that the function is not visible outside the source file. Therefore, if we need to modify the function, we can do so without worrying about possible side effects on other source files.

  • Reduction of possible Name Clashes:

    A Name Clash, which we can translate into Italian as conflict of names, occurs when two functions with the same name are defined in two different source files.

    If we declare a static function, since it is not visible outside, we can reuse its name to define another function in another source file as long as it is also declared as static.

    This is particularly useful in large programs that are possibly developed by multiple programmers. Or when we use third-party libraries. It could happen, for example, that a library provides a function with a name that we have already used. If we declare our function as static, we can avoid the name clash.

    When the set of functions that make up a program is large, it is easy for name clashes to occur or for us to have to be careful not to use names already used by other functions. In technical jargon, this is called Namespace Pollution, or Pollution of the name space. This is a phenomenon very much felt in C programming, where there is no real namespace mechanism as in other programming languages.

    In C++, instead, this problem has been solved by introducing the concept of namespace.

To recap:

Definition

Functions declared as static

A function declared as static has internal linkage, that is, it can be called only within the source file in which it is defined.

The syntax is as follows:

static return_type function_name(parameters);

Declaring a function as static prevents the possibility of calling it from other source files. However, it is always possible to do so using a function pointer.

Storage classes for function parameters

We close this lesson with a final clarification regarding storage classes for function parameters.

Unlike normal variables, function parameters can only have two storage classes:

  • auto like automatic variables:

    Since auto is redundant, it is never used for function parameters.

  • register:

    Function parameters can be declared as register to indicate to the compiler that the parameter will be used frequently and that it is therefore convenient to store it in a processor register.

    However, the compiler is not obliged to respect this indication. If there are no registers available, the parameter will be stored in memory. Just as in the case of variables.

Other storage classes are not allowed.

In Summary

  • Functions in C language can have the extern and static storage classes.
  • A function declared as extern has external linkage and can be called from other source files.
  • The extern class is the default one for functions.
  • A function declared as static has internal linkage and can be called only within the source file in which it is defined.
  • Declaring a function as static prevents the possibility of calling it from other source files.
  • However, it is always possible to do so using a function pointer.
  • Functions can only have two storage classes for parameters: auto and register.

In the next lesson we will put together everything we have learned about storage classes for variables and functions by providing a summary overview.