Saturday, July 24, 2010

C++: Functions continued...

On our last post, we've talked about what a function is and a basic
declaration of a function. Let's examine functions in more detail.

One of the properties of a function is its return type. This is used to tell
the program which calls the function as to what type of data are expected
from it. For example, if a function's return type is an int, the program
would expect the return value to be an integer so that the result of it can
be assigned to an integer variable. However, not all functions returns
something. There is a special return type called "void" which means that we
want our function to do something instead of returning a result, called a
procedure.

For instance, suppose if I want my function to get my name and age and
display it to the screen. Since we are not looking for a particular value,
this can be written as a procedure - using void as its return type.
The algorithm in English looks like this: I just want to get this function
to capture my name and age then display to the monitor. We don't need any
arguments, since we are just dealing with a procedure (however, sometimes an
argument is needed for a void function as well). The function would ask me
for my name and age (separately, of course), then it should print out the
values entered.
Before we continue with C++ version, let's examine this procedure in more
detail. The first question you'll ask is, "how would this blind guy give his
name and age to a function?" The answer is almost like using the method
we've known so far: using main() function. however, instead of writing the
whole procedure in main(), I can write what needs to go into main() in this
small function (creating variables, inputs and outputs and so forth).
Secondly, you may ask, "how come this dude didn't say anything about return
value?" Logically, if the function is a procedure, we don't need anything to
pass back (return) to the calling program, right? Now I think you can
envision the C++ version of this function...
// A function to capture my name and age then displaying it on a monitor.
void my_intro()
{
cout << "What is your name?" << endl;
string name; // My name.
cin >> name;
cout << "What is your age?" << endl;
int age; // My age.
cin >> age;
cout << "I am " << name << ". I am " << age << " years old." << endl;
}
Now, let's write and call this function in a test program:
#include <iostream>
#include <string>
using namespace std;
// A program for testing a small functio...

// A function to ask my name and age then printing it on a monitor.
void my_intro()
{
cout << "What is your name?" << endl;
string name; // My name.
cin >> name;
cout << "What is your age?" << endl;
int age; // My age.
cin >> age;
cout << "I am " << name << ". I am " << age << " years old." << endl;
}

int main()
{
// Call the intro function...
my_intro();

return (0);
}

Very simple, huh? If we do compile this program and run it, we'll get:
Computer: What's your name?
Me: types "Joseph".
Computer: What's your age?
Me: types "20".
Computer: Hi, I am Joseph. I am 20 years old.
Well, how about that? We didn't have to write the actual function body in
our main() function - this saves a lot of space and looks more neat too.

One last thing on this post: Have you noticed that I used braces to mark
that it's a body of a function? I've already mentioned a basic info about
this, but I think this is the first time you are seeing this in its true
form. The braces ({}), as described earlier, is used to denote that we are
in a block of a program. in this context, whenever we run or call a
function, the code from the function (those within the braces) temporarily
takes over, and when we are done with it, control is passed back to the
program which called the function in the first place. As a side effect, we
can call another function from within a function - so that the newer
function takes control from the current function, and when it is done, it
passes control back to the function that called it, which in turn passes
back it's own information back to main(). sounds a bit complicated, but
you'll see this concept again when we work with objects and such. I've even
included a small exercise to illustrate a function calling another function
but you'll meet it later.

More to come later...
// JL P.S. Again, I know it was not easy explaining this stuff... sorry
about that...

No comments:

Post a Comment