Monday, February 23, 2009

Learn Functions More-Part 1

One desirable approach to programming is to modularize your system. One approach is to use functions or procedures or modules whatever is the term used in the language you are in.

In C or C++, we call this modular structures as functions. We have at least one function in C or C++, the main( ). Regardless of the number of functions defined, this main function is always executed first.

What is a function?

-Schi (1992) defined this as, "the building of C in which all program activity occurs"
-It is also called as a subprogram/subroutine that performs a specific task, operation, or computation that may return or not a value.
-It is executed through a function call.
-Its general syntax is this:

function declaration is this:
function_datatype functionname(parameters);

Function definition is this:
function_datatype functionname(parameters)
body_of_ the_function;

Sample program:

[+/-] show/hide

#include stdio.h /*enclose this library in between <> */
/*function declaration at this line */
void greet1();
void greet2();

clrscr(); /* also a function call to clear the screen */
greet1(); /* a function call to greet1 */
greet2(); /* a function call to greet2 */

/*function defintion at this line */
void greet1()
printf("Hello there!\n");

void greet2()
printf("Welcome to modular programming!);

The expected output:
Hello there!
Welcome to modular programming!

No comments: