ECE-1021

Basic Function Format

(Last Mod: 27 November 2010 21:38:43 )

ECE-1021 Home



Objectives


Basic Function Format

A C program is really nothing more than a set of functions. The only thing that can exist outside of a function are data definitions and declarations. All executable code must appear within a function. Furthermore, each function must be separate from all other functions, meaning that you can't define a function within the body of another function.

All functions have a name and there must be exactly one function in the program with the name main(). This is the function that the operating system will call when the program is launched.

A given function consists of declarations for data to be used within that function and executable statements.

All functions in C have certain minimum elements in common. A function definition consists of two parts - the function header and the function body. The function header, in turn, consists of three elements although the first is technically optional. The function body only has one required element but is pretty useless without more.

The Function Header

The function header for our main() function is:

int main(void)

The 'int' is the "return type" of this function. In this case, we are saying that, when it is finished executing, the function will return a value of type 'int' to whatever code called it. It the case of main(), the calling code is the operating system.

The next word is the name of the function, in this case "main". A function name can consist of any combination of alphanumeric characters plus the underscore character subject to the constraint that it can't begin with a number. Function names, like everything else in C, is case sensitive. Every program has to have exactly one function called main() - this is the function that the operating system will invoke whenever you launch the program.

The final element is the parameter list. This is a list of variable types and names, separated by commas, and enclosed by a pair of parentheses. If a function does not take any arguments, then it is said to have a "void parameter list" and this is communicated to the compiler by using the keyword "void" as the sole contents of the parameter list.

The Function Body

The function body is a block of code that immediately follows the function header and that is surrounded by a pair of curly braces. When called, execution begins at the top of this block of code and progresses until either a return statement or the ending curly brace is encountered, which ever comes first. If the function returns a value, such as an 'int' in the case of our main() function, this value is provided immediately after the return statement. It is generally considered poor practice to let the function encounter the ending curly brace because you didn't provide a return statement.


Compound Statements

The curly braces denote what is referred to as a "compound statement". A compound statement is nothing more than a collection of statements that are grouped together and, for flow control purposes, treated as a single statement. In general, the various program flow control statements that we will explore in later lessons control the execution of a single statement - but since that statement can be a compound statement, we can actually have them control arbitrarily large and complex blocks of code. In a similar fashion, all function bodies consist of a single statement - namely one compound statement.


The return Statement

As mentioned previously, every function should end with a return statement. If the function is a void function - meaning that the return type is "void" - then the return statement should simply be the keyword "return" followed by a semicolon. If the function is of any other type, such as 'int' for our main() function, then the appropriate value - or an expression that evaluates to an appropriate value - should be placed between the keyword "return" and the semicolon.

In what is known as a "hosted implementation" - meaning an environment where your program is operating under the control of an operating system as opposed to a "freestanding implementation" such as on the microcontroller in a coffee maker - the main() function is required to return a value of type 'int'. This explicit requirement is a recent addition to the latest C standard (C99) meant to overcome decades of sloppy coding practice on the part of programmers that sometimes had major implications when a program was ported from one machine to another.

By convention, the value returned by main() should be zero if everything worked as expected and something other than zero if any kind of an abnormal situation is causing the exit. The exact value use can be trapped and used to diagnose what the problem is.

There are two symbolic constants defined in the Standard Library library <stdlib.h> intended to support this based philosophy - EXIT_SUCCESS and EXIT_FAILURE.