Saturday, July 24, 2010

C++: Program within a program: introductory notes on functions

When we think of functions, we think of various things. For mathematicians,
it means representing input and output, or describing a relationship. For
others, it may mean how something works, as in the phrase, "the DVD player
has functions to convert video to audio." For programmers and students who
are learning a programming language, functions has a surprising meaning: a
mini program that does something to perform a task. In this part, we'll dive
more into functions and how it works, as well as tricks and different types
of functions.

From the very beginning of our adventure, we've dealt with a function
already - the int main() function, which is the main program sequence. Also,
you may recall that I talked a lot about functions and gave you a preview
sort of notes in the post about vectors. In our context, a function is
defined as a "blackbox" that does whatever to help users use a program.
Functions may perform a wide range of tasks, including converting one unit
to another, calculating derivatives and series, playing a media file to even
closing a nonresponsive program.

There are different types of functions, including the int main() that we've
seen so far, a method (void) that does whatever without returning anything
and recursive functions which calls itself repeatedly. We'll meet these
advanced types later as we go through this "functional adventure."

To begin with, a function requires at least four things: a return type, the
name of the funciton, the argument and the actual return value. A return
type specifies what the resulting value would be when we are done with a
function. The function name is the one that we'll use in our program to
call, or instruct this function to do something when required (the signature
of a function, sort of). An argument is just data that a function will use
to process something. A return value is the actual result of a function that
the "host" program needs.

For example, suppose if we are writing a function to convert from kilometers
to miles. The required things would be: a return type, which in this case
would be a decimal number, the name of the function (let's call it km2mi),
the argument - in our case, the kilometer value and the actual value that
should be returned after the conversion is done. In English terms, the
function would do the following:
Take the orignal kilometer value as the parameter, or the argument. Then
using this value, convert this value to miles (km times 1.6). Then return
this new mile value (a decimal value) to our program.
Sounds complicated, isn't it? But when we do translate this into C++, it
makes more sense (at least for me). Let's see if you can grasp the
translation from English to C++:
// A function to convert from kilometers to miles:
double km2mi)int km)
{
return (km * 1.6);
}
Ah, seems interesting. We've just did our conversion in the return statement
(multiplying the argument by 1.6). This is just a short function, just to
convert from one unit to another, as you've seen from our English
explanation.

To better understand what I am saying, I think it's appropriate to plug this
function to a test program:
// A unit converter...
#include <iostream>
#include <string>
using namespace std;

int main()
{
// Convert from KM to miles.
int km; // The kilometer value.
double mi; // Mile value.
cout << "Enter distance in kilometers:" << endl;
cin >> km;
mile = km2mi(km);
cout << km <, " kilometers = " << mile << " miles." << endl;
return (0);
}

Please pay special attention to the line:
mile = km2mi(km);
The thing we are doing is called "calling a function," or invoking a
function to do our task. The function name is followed by parentheses with
the km argument inside. In our context, the variable "mile" is assigned to
the result, or the return value of our km2mi conversion function.

I think this would give you at least how a function's syntax looks like. In
the following posts, I'll go into different types of return type, methods
versus functions and why we use braces ({}) to surround the body of a
function. In addition, I'll give you advice (at least what my professor
taught me) about function length, using function signatures and so forth -
to lay the foundational knowledge for future adventures.

P.S. If you find that this post is hard to understand, I apologize. If you
have comments, please let me know.
// JL

No comments:

Post a Comment