home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Datafile PD-CD 3
/
PDCD_3.iso
/
languages
/
c
/
c_tutor
/
CHAP5_TXT
< prev
next >
Wrap
Text File
|
1987-11-21
|
34KB
|
785 lines
Chapter 5 - Functions, variables, and prototypes
OUR FIRST USER DEFINED FUNCTION
Load and examine the file SUMSQRES.C for an example of
a C program with functions. Actually this is not the first
function we have encountered because the "main" program we
have been using all along is technically a function, as is
the "printf" function. The "printf" function is a library
function that was supplied with your compiler.
Notice the executable part of this program which begins
in line 8. It begins with a line that simply says
"header()", which is the way to call any function. The
parentheses are required because the C compiler uses them to
determine that it is a function call and not simply a
misplaced variable. When the program comes to this line of
code, the function named "header" is called, its statements
are executed, and control returns to the statement following
this call. Continuing on we come to a "for" loop which will
be executed 7 times and which calls another function named
"square" each time through the loop, and finally a function
named "ending" will be called and executed. For the moment
ignore the "index" in the parentheses of the call to
"square". We have seen that this program therefore calls a
header, 7 square calls, and an ending. Now we need to define
the functions.
DEFINING THE FUNCTIONS
Following the main program you will see another program
that follows all of the rules set forth so far for a "main"
program except that it is named "header()". This is the
function which is called from within the main program. Each
of these statements are executed, and when they are all
complete, control returns to the main program.
The first statement sets the variable "sum" equal to
zero because we will use it to accumulate a sum of squares.
Since the variable "sum" is defined as an integer type
variable prior to the main program, it is available to be
used in any of the following functions. It is called a
"global" variable, and it's scope is the entire program and
all functions. More will be said about the scope of
variables near the end of this chapter. The next statement
outputs a header message to the monitor. Program control
then returns to the main program since there are no
additional statements to execute in this function.
It should be clear to you that the two executable lines
from this function could be moved to the main program,
replacing the header call, and the program would do exactly
the same thing that it does as it is now written. This does
Page 29
Chapter 5 - Functions, variables, and prototypes
not minimize the value of functions, it merely illustrates
the operation of this simple function in a simple way. You
will find functions to be very valuable in C programming.
PASSING A VALUE TO A FUNCTION
Going back to the main program, and the "for" loop
specifically, we find the new construct from the end of the
last lesson used in the last part of the "for" loop, namely
the "index++". You should get used to seeing this, as you
will see it a lot in C programs.
In the call to the function "square", we have an added
feature, namely the variable "index" within the parentheses.
This is an indication to the compiler that when you go to
the function, you wish to take along the value of index to
use in the execution of that function. Looking ahead at the
function "square", we find that another variable name is
enclosed in its parentheses, namely the variable "number".
This is the name we prefer to call the variable passed to
the function when we are in the function. We can call it
anything we wish as long as it follows the rules of naming
an identifier. Since the function must know what type the
variable is, it is defined following the function name but
before the opening brace of the function itself. Thus, the
line containing "int number;" tells the function that the
value passed to it will be an integer type variable. With
all of that out of the way, we now have the value of index
from the main program passed to the function "square", but
renamed "number", and available for use within the function.
This is the "classic" style of defining function variables
and has been in use since C was originally defined. A newer
method is gaining in popularity due to its many benefits and
will be discussed later in this chapter.
Following the opening brace of the function, we define
another variable "numsq" for use only within the function
itself, (more about that later) and proceed with the
required calculations. We set "numsq" equal to the square
of number, then add numsq to the current total stored in
"sum". Remember that "sum += numsq" is the same as "sum =
sum + numsq" from the last lesson. We print the number and
its square, and return to the main program.
MORE ABOUT PASSING A VALUE TO A FUNCTION
When we passed the value of "index" to the function, a
little more happened than meets the eye. We did not
actually pass the value of index to the function, we
actually passed a copy of the value. In this way the
original value is protected from accidental corruption by a
Page 30
Chapter 5 - Functions, variables, and prototypes
called function. We could have modified the variable
"number" in any way we wished in the function "square", and
when we returned to the main program, "index" would not have
been modified. We thus protect the value of a variable in
the main program from being accidentally corrupted, but we
cannot return a value to the main program from a function
using this technique. We will find a well defined method of
returning values to the main program or to any calling
function when we get to arrays and another method when we
get to pointers. Until then the only way you will be able
to communicate back to the calling function will be with
global variables. We have already hinted at global
variables above, and will discuss them in detail later in
this chapter.
Continuing in the main program, we come to the last
function call, the call to "ending". This call simply calls
the last function which has no local variables defined. It
prints out a message with the value of "sum" contained in it
to end the program. The program ends by returning to the
main program and finding nothing else to do. Compile and
run this program and observe the output.
NOW TO CONFESS A LITTLE LIE
I told you a short time ago that the only way to get a
value back to the main program was through use of a global
variable, but there is another way which we will discuss
after you load and display the file named SQUARES.C. In