Tuesday 12 February 2013

Structure of C Functions - C Programming Tutorials


Single Level Functions

The following function is a simple function of one level :

main()
{
//print a message
printf("Welcome to C");
}

Here,
main() - is always the first function to be executed by the computer and should be present in all programs. It is like any other function available in the C library, or like one developed by a user. Parenthesis () are used for passing parameters (if any) to a function.
{ } - mark the beginning and end of a function and are mandatory in all functions.

Multiple Level Functions

The following is an example showing functions at multiple levels - one being called by another (like a subprogram being called by a main program in FoxBASE+).

main()
{
//print a message
printf("Welcome to C.");
disp_msg();
printf("for good learning");
}
disp_msg()
{
//print another message
printf("All the best ");
}

The output of this program is:

Welcome to C. All the best for good learning

In this program:

main() - is a function written by programmers but has to be called main() since it is the first function to be executed.

disp_msg() - is a programmer-defined function that can be independently called by any other function. The () can be used for passing values to functions, depending on whether the receiving function is expecting any parameter.

As memtioned earlier, executable lines are terminated by a simi-colon(;). A statement calling/executing a function needs to be terminated by a semi-colon, but a statement marking the beginning of a function-definition does not have a semi-colon at the end.

0 comments:

Post a Comment

Powered by Blogger.