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(PARAMETERDEF1, PARAMETERDEF2,?)

{

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?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?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;I


Here the declaration  float list[] tells C that list is an array of float.

Note we do not specify the dimension of the array when it is a parameter of a

function.


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



VOID PRINTTABLE(INT XSIZE,INT YSIZE,
FLOAT TABLE[][5])

{ INT X,Y;

FOR (X=0;X


Here float table[][5] tells C that table is an array of dimension of float.

Note we must specify the second (and subsequent) dimension of the array BUT

not the first dimension.


RECURSION IN C


In C, this takes the form of a function that calls itself.


A useful way to think of recursive functions is to imagine them as a process

being performed where one of the instructions is to "repeat the process".


This makes it sound very similar to a loop because it repeats the same code,

and in some ways it is similar to looping.


On the other hand, recursion makes it easier to express ideas in which the

result of the recursive call is necessary to complete the task.


Of course, it must be possible for the "process" to sometimes be completed

without the recursive call.


One simple example is the idea of building a wall that is ten feet high; if I

want to build a ten foot high wall, then I will first build a 9 foot high

wall, and then add an extra foot of bricks.


Conceptually, this is like saying the "build wall" function takes a height and

if that height is greater than one, first calls itself to build a lower wall,

and then adds one a foot of bricks.


A simple example of recursion would be:


VOID RECURSE()
{
RECURSE(); /* FUNCTION CALLS ITSELF */
}

INT MAIN()
{
RECURSE(); /* SETS OFF THE RECURSION */
RETURN 0;
}



This program will not continue forever, however.


The computer keeps function calls on a stack and once too many are called

without ending, the program will crash.


Why not write a program to see how many times the function is called before

the program terminates?


#INCLUDE

VOID RECURSE ( INT COUNT ) /* EACH CALL GETS ITS OWN COPY OF COUNT */
{
PRINTF( "%D\N", COUNT );
/* IT IS NOT NECESSARY TO INCREMENT COUNT SINCE EACH FUNCTION'S
VARIABLES ARE SEPARATE (SO EACH COUNT WILL BE INITIALIZED ONE GREATER)
*/
RECURSE ( COUNT + 1 );
}

INT MAIN()
{
RECURSE ( 1 ); /* FIRST FUNCTION CALL, SO IT STARTS AT ONE */
RETURN 0;
}



This simple program will show the number of times the recurse function has

been called by initializing each individual function call's count variable one

greater than it was previous by passing in count + 1.


Keep in mind that it is not a function call restarting itself; it is hundreds

of function calls that are each unfinished.


The best way to think of recursion is that each function call is a "process"

being carried out by the computer.


If we think of a program as being carried out by a group of people who can

pass around information about the state of a task and instructions on

performing the task, each recursive function call is a bit like each person

asking the next person to follow the same set of instructions on some part of

the task while the first person waits for the result.


At some point, we're going to run out of people to carry out the instructions,

just as our previous recursive functions ran out of space on the stack.


There needs to be a way to avoid this! To halt a series of recursive calls, a

recursive function will have a condition that controls when the function will

finally stop calling itself.


The condition where the function will not call itself is termed the base case

of the function. Basically, it will usually be an if-statement that checks

some variable for a condition (such as a number being less than zero, or

greater than some other number) and if that condition is true, it will not

allow the function to call itself again. (Or, it could check if a certain

condition is true and only then allow the function to call itself).



A QUICK EXAMPLE:



VOID COUNT_TO_TEN ( INT COUNT )
{
/* WE ONLY KEEP COUNTING IF WE HAVE A VALUE LESS THAN TEN
IF ( COUNT < 10 ) { COUNT_TO_TEN( COUNT + 1 ); } } INT MAIN() { COUNT_TO_TEN ( 0 ); }






This program ends when we've counted to ten, or more precisely, when count is

no longer less than ten.


This is a good base case because it means that if we have an input greater

than ten, we'll stop immediately.


If we'd chosen to stop when count equaled ten, then if the function were

called with the input 11, it would run out of memory before stopping.


Note



That so far, we haven't done anything with the result of a recursive function

call.


Each call takes place and performs some action that is then ignored by the

caller.


It is possible to get a value back from the caller, however.


It's also possible to take advantage of the side effects of the previous call.


In either case, once a function has called itself, it will be ready to go to

the next line after the call. It can still perform operations.


One function you could write could print out the numbers 123456789987654321.


How can you use recursion to write a function to do this? Simply have it keep

incrementing a variable passed in, and then output the variable twice: once

before the function recurses, and once after.


VOID PRINTNUM ( INT BEGIN )
{
PRINTF( "%D", BEGIN );
IF ( BEGIN < 9 ) /* THE BASE CASE IS WHEN BEGIN IS NO LONGER */ { /* LESS THAN 9 */ PRINTNUM ( BEGIN + 1 ); } /* DISPLAY BEGIN AGAIN AFTER WE'VE ALREADY PRINTED EVERYTHING FROM 1 TO 9 * AND FROM 9 TO BEGIN + 1 */ PRINTF( "%D", BEGIN ); }




This function works because it will go through and print the numbers begin to

9, and then as each printnum function terminates it will continue printing the

value of begin in each function from 9 to begin.


This is, however, just touching on the usefulness of recursion. Here's a

little challenge: use recursion to write a program that returns the factorial

of any number greater than 0. (Factorial is number * (number - 1) * (number -

2) ... * 1).


Hint:


Your function should recursively find the factorial of the smaller numbers

first, i.e., it takes a number, finds the factorial of the previous number,

and multiplies the number times that factorial...have fun. :-)


FUNCTION 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.


CALL BY VALUE AND CALL BY REFERENCE


In C programming language, variables can be referred differently depending on

the context.


For example, if you are writing a program for a low memory system, you may

want to avoid copying larger sized types such as structs and arrays when

passing them to functions.


On the other hand, with data types like integers, there is no point in passing

by reference when a pointer to an integer is the same size in memory as an

integer itself.


Now, let us learn how variables can be passed in a C program.


PASS BY VALUE


Passing a variable by value makes a copy of the variable before passing it

onto a function. This means that if you try to modify the value inside a

function, it will only have the modified value inside that function.


One the function returns, the variable you passed it will have the same value

it had before you passed it into the function.


PASS BY REFERENCE


There are two instances where a variable is passed by reference:


When you modify the value of the passed variable locally and also the value of

the variable in the calling function as well.


To avoid making a copy of the variable for efficiency reasons.



Sample Code



#INCLUDE
#INCLUDE

VOID PRINTTOTAL(INT TOTAL);
VOID ADDXY(INT X, INT Y, INT TOTAL);
VOID SUBXY(INT X, INT Y, INT *TOTAL);

VOID MAIN() {

INT X, Y, TOTAL;
X = 10;
Y = 5;
TOTAL = 0;

PRINTTOTAL(TOTAL);
ADDXY(X, Y, TOTAL);

PRINTTOTAL(TOTAL);

SUBXY(X, Y, &TOTAL);
PRINTTOTAL(TOTAL);
}

VOID PRINTTOTAL(INT TOTAL) {
PRINTF("TOTAL IN MAIN: %DN", TOTAL);
}

VOID ADDXY(INT X, INT Y, INT TOTAL) {
TOTAL = X + Y;
PRINTF("TOTAL FROM INSIDE ADDXY: %DN", TOTAL);
}

VOID SUBXY(INT X, INT Y, INT *TOTAL) {
*TOTAL = X - Y;
PRINTF("TOTAL FROM INSIDE SUBXY: %DN", *TOTAL);
}


There are three functions in the above program. In the first two functions

variable is passed by value, but in the third function, the variable `total`

is passed to it by reference. This is identified by the “*” operator in its

declaration.


The program prints the value of variable `total` from the main function before

any operations have been performed. It has 0 in the output as expected.


Then we pass the variables by value the 2nd function addxy, it receives a copy

of `x`, `y`, and `total`. The value of `total` from inside that function after

adding x and y together is 15.


Notice that once addxy has exited and we print `total` again its value remains

0 even though we passed it from the main function.


This is because when a variable is passed by value, the function works on a

copy of that value.


They were two different `total` variables in memory simultaneously. The one we

set to 15 in the addxy function, was removed from memory once the addxy

function finished executing.


However, the original variable `total` remains 0 when we print its value from

main.


Now we subtract y from x using the subxy function. This time we pass variable

`total` by reference (using the “address of” operator (&)) to the subxy

function.



Note


How we use the dereference operator “*” to get the value of total inside the

function. This is necessary, because you want the value in variable `total`,

not the address of variable `total` that was passed in.


Now we print the value of variable `total` from main again after the subxy

function finishes. We get a value of 5, which matches the value of total

printed from inside of subxy function.


The reason is because by passing a variable total by reference we did not make

a copy of the variable `total` instead we passed the address in memory of the

same variable `total` used in the function main.


In other words we only had one total variable during entire code execution

time


No comments:

Post a Comment