## Monday, 2 May 2011

### Functions

Functions

C provides functions which are again similar most languages. One difference is that C regards main() as function. Also unlike some languages, such as Pascal, C does not have procedures -- it uses functions to service both requirements.

Let us remind ourselves of the form of a function:

returntype fn_name(1, parameterdef2,$\cdots$)

{

localvariables

functioncode

}

Let us look at an example to find the average of two integers:

float findaverage(float a, float b)
{
float average;
> average=(a+b)/2;
return(average);
}

We would call the function as follows:

main()
{
float a=5,b=15,result;
result=findaverage(a,b);
printf("average=%f$\setminus$n",result);
}

Note: The return statement passes the result back to the main program.

void functions

The void function provide a way of emulating PASCAL type procedures.

If you do not want to return a value you must use the return type void and miss out the return statement:

void squares()
{
int loop;
for (loop=1;loop<10;loop++); printf("%d$\setminus$n",loop*loop); } main() { squares(); } NOTE: We must have () even for no parameters unlike some languages. Functions and Arrays

Single dimensional arrays can be passed to functions as follows:-

float findaverage(int size,float list[])

{ int i;
float sum=0.0;

for (i=0;iFunction Prototyping

Before you use a function C must have knowledge about the type it returns and the parameter types the function expects.

The ANSI standard of C introduced a new (better) way of doing this than previous versions of C. (Note: All new versions of C now adhere to the ANSI standard.)

The importance of prototyping is twofold.

* It makes for more structured and therefore easier to read code.
* It allows the C compiler to check the syntax of function calls.

How this is done depends on the scope of the function (See Chapter 34). Basically if a functions has been defined before it is used (called) then you are ok to merely use the function.

If NOT then you must declare the function. The declaration simply states the type the function returns and the type of parameters used by the function.

It is usual (and therefore good) practice to prototype all functions at the start of the program, although this is not strictly necessary.

To declare a function prototype simply state the type the function returns, the function name and in brackets list the type of parameters in the order they appear in the function definition.

e.g.

int strlen(char []);

This states that a function called strlen returns an integer value and accepts a single string as a parameter.

NOTE: Functions can be prototyped and variables defined on the same line of code. This used to be more popular in pre-ANSI C days since functions are usually prototyped separately at the start of the program. This is still perfectly legal though: order they appear in the function definition.

e.g.

int length, strlen(char []);

Here length is a variable, strlen the function as before.