Amiga Computing - Commodore Is Awesome
Amiga Computing - Commodore Is Awesome
Amiga Computing - Commodore Is Awesome
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Of you use a photocopier to make a<br />
copy you go to the machine, place<br />
the document to be copied inside,<br />
press a button and wait for the<br />
device to do its job. It's likely you do all this<br />
without really knowing what goes on inside -<br />
you just know what input is required (the doc<br />
ument to be copied) and what must be done to<br />
start the copying process. You also know that<br />
some results will come back i.e. a copy of the<br />
input document.<br />
This information hiding 'black box' concept is<br />
an effective way of protecting a user from<br />
unnecessary complexity. For the C programmer,<br />
there is an equally effective facility - the C func<br />
tion. This provides similar capabilities and<br />
because of this, functions are the essential<br />
building blocks of all C programs.<br />
Many functions, like printfO we used last<br />
month, have been found to be so useful they<br />
are provided with all C compilers. The <strong>Amiga</strong><br />
also has its own function libraries and we'll see<br />
later how these are used for handling Intuition,<br />
graphics and so on. Often however, you'll need<br />
to create your own functions.<br />
Function Definitions<br />
Listing 1 provides a general description of a C<br />
function whilst listing 2 shows a simple exam<br />
ple which calculates the area of a rectangle by<br />
multiplying its width and length values togeth<br />
er. This latter definition uses C's * operator to<br />
indicate multiplication and give us our first<br />
encounter with some variables.<br />
Variables represent areas of memory that<br />
you can use for storing information and, just as<br />
with languages like Basic, it helps to use names<br />
that mean something. Needless to say 1 have<br />
defined a variable called result - because in this<br />
instance that's exactly what it is being used to<br />
store.<br />
The expression 'int', incidentally, is just C's<br />
way of describing a variable that can store an<br />
integer (a whole number) value. The width and<br />
length parameters used in the function defini<br />
tion are also variables (again defined as inte<br />
gers) but, because they represent the function<br />
arguments, take on whatever values are sup<br />
plied when the function is used. Inside the func<br />
tion however, these items are used just like any<br />
other variables.<br />
The return() statement indicates the value to<br />
be returned to the caller and any valid C expres<br />
sion may be used inside the parenthesis.<br />
Functions do not have to provide a return value<br />
and a returnO statement without an expres<br />
sion, or in fact no returnf) statement at all, both<br />
result in no actual value being returned. It's also<br />
perfectly legal to ignore a return value, although<br />
narch^t est^l<br />
iarch_t est . c<br />
rtarchtest.o<br />
More help for<br />
aspiring C coders<br />
as Paul Overaa<br />
puts function use<br />
under the spotlight<br />
un ctj on<br />
return-type funttion-nase ( parameter list )<br />
variable declarations<br />
appropriate C statements<br />
Listing 1: In general C functions<br />
have this sort of layout<br />
in the case of our example function, it would<br />
make little sense to carry out the calculation if<br />
you weren't going to use the results!<br />
So, the listing 2 function definition tells us<br />
Area() is a function that expects two integers<br />
representing the width and length of a rectangle<br />
- and returns an integer representing the area of<br />
that rectangle. Using the function is easy - if, for<br />
example we have a variable called<br />
rectangie_area that we want to set to the area<br />
of a 6 unit by 8 unit rectangle we just write:<br />
rectangle_area = Area(6,8);<br />
C evaluates the right hand side of the expres<br />
sion by making a call to the AreaO function<br />
(using the parameters supplied). Then it assigns<br />
the function's return value to the variable on the<br />
left hand side of the expression. The result - rec-<br />
tangle_area gets set to 48!<br />
To be honest, the operation of multiplying<br />
two numbers together, which is all Area{) does,<br />
would not normally be written as a function -<br />
you'd just work out the result using the state<br />
ment widthiength. It must be said however,<br />
that if you were going to define a function for<br />
pj Output Hindoo : .;_""'.<br />
le^area of a rectaiig I e 5 inches liv 8 inches TIT<br />
48 square inches!<br />
This month's example can be compiled in<br />
exactly the same way as last month's code<br />
<strong>Amiga</strong> <strong>Computing</strong><br />
MARCH 1997<br />
int Areafint width, int length)<br />
I<br />
int result;<br />
result=tndth'length;<br />
returnf result);<br />
Listing 2: An example function definition<br />
doing this you could do it more concisely by<br />
using the function's arguments directly:<br />
int Area(in: iiidth, int length)<br />
{<br />
retsjrnlwidth'Length);<br />
int Areafint vidtii, int<br />
tength)treturnfnidth*length);}<br />
Notice that C, unlike other languages, isn't par<br />
ticularly fussy about the physical layout of the<br />
source code.<br />
Some Example Code<br />
On the coverdisk you will find a short program<br />
that makes use of this AreaO function along<br />
with a few extra notes about the variable and<br />
function naming conventions I use. One thing<br />
you will notice in this month's source is an ANSI<br />
C function declaration statement called a proto<br />
type<br />
int Areafint width, int length);<br />
This looks pretty much like the first line of the<br />
function description itself, with a semicolon<br />
tagged on the end. It's job is to provide an ini<br />
tial definition of the variable types being used<br />
which then allows the compiler to check all<br />
subsequent function use is correct (pre-ANSI<br />
compilers were not able to do this).<br />
Needless to say there is plenty more to be<br />
said about functions but it's best to deal with<br />
individual issues as they crop up within exam<br />
ple code. So, although next month it's C vari<br />
able types and operators that come under the<br />
limelight, you will in fact be also be learning a<br />
little more about function use as well. /^*f