Sunday, July 25, 2010

C++: Few more function notes and practice exercises

As we wrap our discussion on basics with function, I'd like to point out few
opinions, styoes and miscellaneous ideas about functions. Then, we'll dive
into some practice exercises with functions to put our knowledge into

As for function arguments, you can put anything in there - an integer,
double, char, string, vectors, arrays, and even our own data types. A
function argument, as described earlier, is the data that a function will
use to process something. A function can have anywhere from no argument to
four or five (or more). When we speak of arguments, we say a program passes
an argument to a function. For example, for our kilometer to mile converter,
the argument would be the km value, and when main() cllas our conversion
function, it'll pass an integer value (named km) to our converter so that it
can process it.

As a side note, you don't really have to use same variable nemas as the ones
used in function arguments. For instance, back in the converter function,
we've declared an int argument called "km." However, in the main, you can
write other variables as long as it is an integer e.g. if we pass in, say,
kilo as our argument to our function, the function can work with it since
it's the same data type as the argument itself.

Speaking of passing in an argument, we can use two methods: passing in by
value, or using a reference (an address of a value). The chief difference
(at least how we write it) is that there is an additional character that we
use when passing in a value - an ampersand (&), which is called "address of"
operator. In this context, passing in by value means we want to change the
actual value of the argument, whereas passing by reference means we don't
really have to change the values stored within that address (this becomes
important as we design our own classes).

Another note to consider: It is recommended (from Practical C++ Programming
and from my professor) that a function's body should not exceed no more than
ten to fifteen lines of code. If this happens, it'll be hard to follow what
a function is doing, thus my professor recommends splitting a long function
to two or more smaller (helper) functions. This may seem not an easy thing
to follow (especially for me), but when we need to look up a body of a
function quickly to skim through its process, a very long function body
would be a waste of time and space on an editor window. Besides, splitting a
long function into smaller ones helps us reuse our code, thus saving time
and complexity. I'll return to this concept later when we dive more deeply
into functions.

So, I'm sure you now have a basic grasp of what a function is, how it works,
components and uses. Thus, let's put our theories into practice by writing a
few simple code:

1. suppose if we want to create a function that'll print out a vector. This
version is an enhancement of our vector output routine that we've seen
before. First, describe in English what the components of this function
would consist of - return type, name, possible arguments, return value (if
any) and algorithms to print out a vector. (hint: actually, you don't need
to return anything since we are using a lop to do this). After this, come up
with a possible C++ version of your English description.

2. Create a program to check if you are old enough to drink alcohol. In
this one, try to come up with at least two functions: one to ask your name
and age, and another one (called by the first function) which takes your age
as an argument to decide whether you are old enough (over 21) to consume
alcohol. Then call this first function in the main() function.
Few hints:
A. Since we are doing a procedure, what would be the return type for the
first function?
B. In the second function, how to we test to ensure that the user is over 21
or not, and what variable do we need to assign the return value as a true or
false statement?
This one sounds a bit complicated, so I'll post some portion of the program.

3. Create a function to convert from Celcius to Fahrenheit, call this
function in the main() and assign a double variable, "temperature" to the
result of this function i.e. the value that the function returns. Note that
you may need to use operator precedence (parenthesis) to helop you out
(foruma = -40 + (1.8 times celcius)). When we run this program, the computer
should ask for a temperature in celcius, the user types it and the function
is called to convert this from Degrees C to Degrees F and display the

The result of these exercises will be posted later along with a functioning
program (except number 2 which requires an extensive coverage).

I hope the series on basics of functions was easy enough for you to
understand (and correct). If there are any mistakes, omissions,
misinformation and comments, please let me know (so that I can learn from it
and correct it)...
// JL P.S. Off to summer school at UCR...

No comments:

Post a Comment