Return to sysprog.net home page

españolfrançaisdeutsch

C and C++

Main page

Defines

Design

Loops

Pointers

Punctuation

Strings

Stuctures

Types


SITE SERVICES
Contact Us
Search
Site Map


Home —» C and C++> —» program design


C Tutorial: Program Design


Assembler, COBOL, PL/I and Fortran are all procedural languages: each program contains a series of procedures (or subroutines) that modify the variables in the program's working storage.

C is a functional language: each function (or subroutine) modifies only the variables in its own work area. C++ is an 'object-directed' language: you can write it either like C or using an object-oriented approach.

The table below displays the general format of programs in assembler, C and COBOL. Each model program has an entry point, variables, and a single subroutine. The color-coding maps similar functions in each language.

Assembler C COBOL


Global variables
   START int main() PROCEDURE DIVISION.
   BAS R14,HERE here(); PERFORM HERE.



HERE EQU * void here() HERE.

Local variables
   BR R14 return;



Global Variables

Variables: global or local
The one difference that immediately stands out is that assembler and COBOL have one large workarea that can be read and updated by any line of code in the program. C programs usually have no global variables: each function has its own workarea. The variable definitions always precede the code in the function: this is required by the one-pass C compiler.

Entry Point
All C programs have only one entry point at a statement called main(). The 'int' says that the program can pass back a return code. The compiler won't care if you omit this keyword, because it's the default.

Calling a Function
Functions are called by stating the function name followed by parentheses. It's the parentheses that tell the compiler that this is a request to perform a function. Unlike assembler, you can't branch to a label in the middle of a function: you must always enter the function from the top.

Exiting a Function
The function will be performed until it meets a 'return' statement or the end of the function. Usually programmers code a return statement at the end of each function for clarity. If you omit it, the compiler will insert an implicit return. You won't drop through to the code below, as you would with assembler.

Passing a Parameter List
Since C functions don't share a common variable pool, you need to pass a called function any variables it needs in a parameter list. Suppose you want to call function CheckInventory and pass it a parts number and count. The call to the function would be:

CheckInventory(partnumber, count);

The argument list must match the function's parameter list, in this case a string followed by an integer:

void CheckInventory(char *id, int number)

Returning a Value
C functions are allowed to return a maximum of one variable. If the function doesn't need to return a variable, then it is defined with a type of 'void', as shown above. But if CheckInventory needs to return the number of items that will be back-ordered, the call will be:

backorder = CheckInventory(partnumber, count);

And the function header will show that it returns an integer:

int CheckInventory(char *id, int number)

Prototyping a Function:
The compiler will check that the arguments list in the function call matches the parameter list the function is expecting. Since the one-pass compiler may reach the function call before the function definition, you need to provide a function prototype at the top of the program. This is just a copy of the function header, followed by a semi-colon:

int CheckInventory(char *id, int number);

Prototypes for standard library functions - like printf - are obtained by including the appropriate header file.


TOP OF PAGE C is quirky, flawed and an enormous success. (Dennis Ritchie)