Placement and Visibility of Functions

Summary: A function is normally visible everywhere in the program.

Every C function is normally “visible” to all other functions in the same program. That is, it can call and be called by any other function. C functions can even call themselves, a process known as “recursion.”

In the following programs, the functions whiz and bang are visible to main and to each other. The main function can call both whiz and bang. In addition, whiz can call bang, and vice versa.

main()

{

}

whiz()

{

}

bang()

{

}

Functions can appear in any order and at almost any place in your program. Since main starts and ends the program's execution, this function often begins the program. But this is a readability convention, not a language requirement.

Summary: C functions can't be nested.

One place where you can't put a function is inside another function. The C language doesn't allow you to nest functions. Here C differs from QuickPascal, in which one procedure can contain other “hidden” functions or procedures. The following program causes a syntax error because the bang function appears within the whiz function:

main()

{

}

whiz()

{

/* Error! Incorrect function placement */

bang()

{

}

}