Functions
The built in functions of RobotC, like nxtDisplayString(), are able to perform manytasks. For a number of reasons, however, it is desirable to construct new functions out of
the existing functions. A function is a self-contained collection of instructions that, when
called, accepts a specied number of arguments, performs some task, and returns a value
back to the calling program. There are three main reasons for writing new functions:
1. Procedural abstraction { allows the programmer to think of a program as a collection
of sub tasks and to focus on making each sub-task work correctly before \gluing" them
together to accomplish the complete task.
2. Reusability { allows the programmer to write and debug a set of instructions that
can be used over and over in dierent contexts rather than re-writing the same set of
instructions in numerous places.
3. Readability { even if reusability is not an issue, in large programs it is often desirable
to \encapsulate" instructions for a particular sub task into an aptly named function.
Doing so makes the main part of the program a sequence of function calls that is quite
readable. Encapsulation also helps to isolate programming errors. Once a program
exceeds one hundred lines or so, the programmer should consider using functions to
encapsulate related sections of instructions for the sake of readability.
Function syntax is similar to variable syntax in that a function must be dened before
it is called. The scoping rules of a function, the places in the program where the name of
the function is recognized, are the same as for variables. The naming rules for functions also
match those of variables. The general syntax for a function declaration is
// function comment
[ return type ] [ name ]([ argument list ]) {
// body block , instructions to carry out
// function 's task
}
Like variable declarations, function denitions may occur anywhere, but tradition has the
programmer put all of the function denitions at the top of the program before the start
of the main program block. Tradition also dictates that a descriptive comment above each
declaration describe the purpose, inputs, and outputs of the function.
// renders a simple 10 by 10 smiley face
// on the display centered about the
// coordinates (x,y), no return value
void DisplaySmiley ( int x, int y) {
nxtDrawEllipse (x -5,y+5,x+5,y -5);
nxtSetPixel (x -2,y +2);
nxtSetPixel (x+2,y +2);
nxtSetPixel (x,y -2);
nxtSetPixel (x -1,y -2);
nxtSetPixel (x+1,y -2);
nxtSetPixel (x -2,y -1);
nxtSetPixel (x+2,y -1);
return ;
}
Listing 4.6: A function that draws a 10 10 pixel smiley face centered at the coordinates
(x,y).
Consider, for example, the carefully crafted instructions in Listing 4.6 that use RobotC
commands to create a smiley face on the display.
The obvious advantage of this function is that numerous smiley faces can be positioned
around the display area, but the work of how to render the smiley face relative to the center
coordinates (x,y) only needs to be done once. The descriptive name DisplaySmiley helps
with readability. This function accepts two integer-type arguments. The dummy variables
x and y can be replaced with any integer variable in the calling program or with literal
integers. The return; statement signies the end of the function and causes the program
execution to return to the calling program just after the function call. In this example, the
return statement is not necessary, but it is good practice to include it regardless.
// computes the mean of its two arguments ,
// returns the mean
float Mean ( float a, float b) {
return (a+b )/2;
}
Listing 4.7: A function that accepts two
oat-type arguments, computes their mean, and
returns the result to the calling program.
Consider Listing 4.7 which is an example of a function that computes the average of its
two arguments and returns the result to the calling program. In the calling program, the
function call is literally replaced by the return value. For example, in the calling program,
the instruction
average = Mean(w1,w2);
assigns the mean of the numbers w1 and w2 to the variable average. During execution, the
program recognizes that in order to perform the assignment, it must rst determine the value
of the right-hand side. All expressions and functions on the right-hand side are evaluated
and reduced to a single value before the assignment occurs.
No comments:
Post a Comment