Menu Close

Articles

C: Functions & Pointers

A function is defined as a relation between input parameters and output results. In mathematics a function should be deterministic. Unique input will produce same result. If this is not true than the function is stochastic.

General Concept

The idea of function comes from mathematics but in IT sometimes a function can be just another synonym for sub-routine. A sub-routine is a set of statements encapsulated into a block of code with a name. Sub-routines can have input/output parameters and one or multiple results.

The role of a sub-routine is covered by functions. C do not have a reserved keyword to declare a function. In other languages we have “fn” or “def” or “func” but in C like in Java you specify the name of the function and the result type. So a function in C is not actually a function but a “named section” of code.

Main Function

In every C program you must create function main(). If this function is missing the program can not compile. Function main() will drive the program and usual this function contains a loop that is called the main loop.

Inside the main loop you can display a menu, ask for user input, call other functions, perform computations, display the results and exit. This is the most common structure of a program:

Example:

Function Scope

A function have a local scope. That means you can define variables that exist only inside the function body. After function is ending the local scope is cleared. All variables defined in local scope will be removed from memory.

In contrast to local scope, every C program unit has a global scope. If you define variables outside of any function these variables are visible in all functions, except if you define other local variable with the same name. In this case the global variable can be shadowed by the local variable.

I the function main() defined above you can see local variables: x, y, r and op. Also x =10, y=10 global variables are defined, except these two are unused and have no effect over the computations.

Side Effects

Functions that can read from console, print something or modify global variables. These actions are called side-effects. In example above, function main() has side-effects but function add() and sub() are pure. Do not have side-effects.

Parameters

Functions can receive one or more parameters. These are sometimes called “formal parameters“. Parameters are similar to local variables, except they do not have initial value. The value for the parameter is establish in the function call.

In the example above, functions add() and sub() have each 2 parameters: a, b. List of parameters is separated by comma. For each parameter you must declare data type and provide a “actual value” that is called argument.

Note: C do not have default parameters. Next examples are invalid declarations attempts in C:

Return

A function can use return keyword to create the result. Observe that result type is declared in function definition. It is the first thing before the function identifier. So the compiler will know if your expression used by return will create the right type.

Syntax:

return <expression>;

Where <expression> can be a formula, or a constant literal. If the function do not have return then the function is void. For declaring a void function we use special keyword: void. This has significance of a type that has no value.

Example:

The above example does nothing at all but will terminate with error. Usually a C program uses int main() function and return a number that can be 0 or !=0. If the code is not 0 it means the program has an error defined probably by the error code.

Example:

Function Call

To call a function you must use function name follow by a list of arguments enclosed in parentheses (). If the function has a result then it can be used in expressions. Also function result name can be ignored.

Example:

Output parameters

Before you can understand more about functions and function calls with output parameters we must learn about pointers. A pointer is a variable whose value is the address of another variable.

Pointers can be used to create input/output parameters. Inside the function body you can change content of these parameters and use the arguments to capture multiple output values.

Pointer notation:

A pointer has a type. To declare a pointer we must specify data type first then pointer identifier name with prefix “*”.  That is all the secret. You define a variable with prefix and that is a pointer. 

type_name  *identifier;

Variable address:

Any variable is stored in memory. So it must have an address. This address can be extracted and it can be assigned to a pointer. This way we can create a reference to a particular variable. For extracting the address we use operator “&” that is unary operator. 

pointer_name = &variable;

Example:

Multiple results

A function can have a single result. This result is created with return keyword. Sometimes we need more then one results from a function. In this case we need to define a composite type and use this type as result. Another method to make a function compute multiple results is to use output parameters or to use global variables and side-effects.

You can see two examples here:

Probably you will not understand above examples, be patient and read next articles. After you understand better the composite types and pointers you will be able to create such functions yourself.

Next article: Composite Types.