16)

Program 15 - Functions + parameters

Program 15 Functions + parameters Source code - prog15.c
This next program extends what was done with the last program by introducing return values from functions and parameters. The program itself is one which has been done previously, it works out the area of a circle given the radius. This time though it has been implemented using two useful functions.

In the description, there is a further example program which again uses two functions, this time though, to work out the area of a rectangle.

The clrscr statement is also shown for the first time here, which clears a DOS text screen.

 

Program code- new parts shown in red

#include <stdio.h>

#define PI 3.1427

float getarea(float radius)
{
float area;
area=PI*radius*radius;
return(area);
}

void circlestats(
float radius)
{
float area;
area=getarea(radius);
clrscr();
printf("Radius = %f\n",radius);
printf("Area = %f\n",area);
}

void main(void)
{
float rad;
printf("Enter the radius of the circle\n");
scanf("%f",&rad);
circlestats(rad);
}

 

Description of the program code
There are three functions in this program getarea, circlestats and main. The main as ever is the start of the program, and all it does is ask the user to enter the radius of a circle, which is stored in the rad variable. It then goes on and calls the circlestats function, but this time with a difference. Instead of the following brackets being empty they contain the variable rad : circlestats(rad); This is called passing a parameter to a function. The parameter is the value of the rad variable.

The definition of the circlestats function is different as well from previous examples, the (void) has been replaced with (float radius). Radius becomes another variable which can only be seen and used by the circlestats function. The value of the radius variable in the circlestats function is initially set to the value passed to the function ( in this case the value of the variable rad in the main function ).

The circlestats function then executes, in the process calling the getarea function with the value of the radius variable being passed as a parameter to it. The call to the getarea function is not the same as previous examples either, because area= has been placed in front of it. Forget for a second about the area= part, and look at the getarea function.

The getarea function accepts a single parameter like the circlestats function, it also has the same name radius. The value of this variable, like the circlestats function is set to whatever value is passed into the function from the calling function. Note that even though the circlestats and the getarea have a parameter with the same name, they are different variables, although in this example they both have the same value.

Within the getarea function the normal PI rē circle area calculation is done, with the result stored in a "local variable" called area. The term "local variable" has been used for the first time here, although local variables have been used all along. The reason they are called local variables is because they are only recognised and see in the local function that defined them. It is possible to have "global variables" that can be seen by all functions in a program, as will be seen later.

The different things to note about the getarea function is that the inital void has gone and been replaced with float : float getarea(float radius) and also there is a line which reads return(area); at the end of the function. Any function that does not have void at the start, must have a return line. This is because functions declared in this way are set to return a value to the caller, in this case the value of the area variable that's been calculated.

The caller in this case is the area=getarea(radius); line in the circlestats function. That is why the line to call the function looks so different, the value that is being returned by the function will be stored in the area variable local to the circlestats functions. It gets a bit mind twisting with functions, and difficult to explain ! The program code probably makes a lot more sense than the description here, but it's a lot easier to program than write or talk about it !

So the general format of the first line of a function is :

<return type> <function name>(<parameters>)

If void is specified for the return type, this indicates that the function doesn't return a value. If void is specified instead of any parameters, this indicates that the function accepts no incoming parameters to it. Notice the use of "parameters", not parameter. This is because it is possible to pass more than one parameter to a function, as is shown by the program below, which calculates the area of a rectangle ( width x height ).

#include <stdio.h>

float getarea(float width
,float height)
{
float area;

area=width*height;
return(area);
}

void rectstats(float width
,float height)
{
float area;

area=getarea(width,height);
clrscr();
printf("Rectangle = %f x %f\n",width,height);
printf("Area = %f\n",area);
}

void main(void)
{
float width,height;

printf("Enter the radius of the rectangle\n");
scanf("%f%f",&width,&height);
rectstats(width
,height);
}

Earlier, when variables were introduced, it was stated that you couldn't have two variables with the same name. This now isn't entirely true as it is possible to have two variables with the same name, but not in the same function.

This is where things start to get complicated with variables, and a lot of new programmers take the cop out approach and use "global variables". This is the reason global variables have not been introduced yet, because it is possible to write any program without using them. It's not very practical though, so there is still a need for an explanation on the next page.

Functions can only return one value though, they can accept as many parameters as you like into the function, but can only return a maximum of one value. There is a way around this though by using pointers, which are covered later.

The final new part is the use of clrscr(); in comparison to functions and parameters, this is easy, it simply clears a DOS text screen.

 

Summary
Another difficult program to get to grips with, but it's coming to a point where C programming is nearly covered in it's entirity !

Functions, especially where parameters and return values are used are really helpful, especially when building up "libraries" of code. A library is a collection of common functions that are used. A programmer will build up a library of common useful functions that will be used over and over in different programs to save re-writing each one everytime.

The best way to understand functions is to use them, and after a while, you wonder how you could live without them, especially as programs start to get bigger. They can make a large program difficult to trace through and follow though at times, which is the downside to them. The majority of the following programs will be using functions with parameters and return values, so it is important to understand them before moving on, if the description above doesn't make sense, slowly going through the two programs should.

 

Tasks

15.1) Write a program that converts fahrenheit to celcius again, but this time use a function that does the work by accepting the fahrenheit as a parameter into the function and returns the celcius conversion.

15.2) Write a program that asks the user to enter a weight in lb's oz's and dr's and outputs the wieght in drams. A function should be used, that has three parameters coming into the functions ( lb's oz's and dr's ) and returns a single value indicating the drams.

 

Go back a page Continue on to next page >>

(c) J.C.Spooner 2001