bg287

csc110: C++ Functions and Procedures, Part I

Posted in csc110 by bnmng on 2010 03/25

My take on what we learned today

Say you have a program like this:
This program twice asks for a number between 1 and 10. If the user enters a something bigger than 10 or smaller than 1, it insults the user.

#include <iostream>
#include <iomanip>

using namespace std;
 
int main()  
	{
	int onetoten =0;
	int anotheronetoten=0;

	cout << "Enter a number from 1 to 10" << endl;
	cin >> onetoten;
 
	if (onetoten>=1 && onetoten<=10) {
		cout << "great, you can follow instructions" << endl;
	} else {
		cout << "You're an Idiot!" << endl;
	}
	
	
	cout << "Enter another number from 1 to 10" << endl;
	cin >> anotheronetoten;

	if (anotheronetoten>=1 && anotheronetoten<=10) {
		cout << "great, you can follow instructions" << endl;
	} else {
		cout << "You're an Idiot!" << endl;
	}
	
	cin.get() // I use this instead of system(“PAUSE”)  
	cin.get() // because “PAUSE” is an MS Windows command
	return 0;
}

As you see, you there’s some redundancy. It would be nice if you could take some of the redundancy out. Here’s how:

#include <iostream>
#include <iomanip>

using namespace std;

void checknumber(int);

int main()  
	{
	int onetoten =0;
	int anotheronetoten=0;

	cout << "Enter a number from 1 to 10" << endl;
	cin >> onetoten;

	checknumber(onetoten);

	cout << "Enter another number from 1 to 10" << endl;
	cin >> anotheronetoten;

	checknumber(anotheronetoten);

	cin.get(); // I use this instead of system(“PAUSE”)  
	cin.get(); // because “PAUSE” is an MS Windows command 
	return 0;
}

void checknumber (int thenumber) 
{
	if (thenumber>=1 && thenumber<=10) {
		cout << "great, you can follow instructions" << endl;
	} else {
		cout << "You're an Idiot!" << endl;
	}
}


In this example, I might not have actually saved any typing because the program is pretty simple. As your programs get more complex, you’re going to appreciate the advantages of taking some of your code outside of the main() routine and placing them in separate functions or procedures (I wrote about the terminology of “functions” and “procedures” in my next post).

I took the lines that check the number and insult the user and I put them in a procedure which I called “checknumber”. When I wanted to use it, I called it from inside main.

At the top of the program I wrote:

	void checknumber(int);

That was the prototype. Prototypes don’t seem to have any purpose but we have to have them in C++ . They actually do have a purpose, but for now just accept the fact that we need them. Some programming languages don’t require them.

In main() I wrote:

	checknumber(onetoten);


and later, still in main(), I wrote

	checknumber(anotheronetoten); 

Those were the calling statements. At that point, the program flow jumped out of main(), jumped to the checknumber procedure, ran the code in that procedure, then jumped back to line after the call. When the program jumped from the calling statement to the procedure, it took the value of the argument onetoten with it, the first time, and the value of anotheronetoten the second time.

Below main(), I wrote:

	void checknumber (int thenumber)


And under that I wrote the body of the procedure. This is the definition. The definition includes the header and the body. thenumber took the value of the argument that was passed to it.

It’s important to note:
In the prototype, you only need to describe the arguments, you don’t have to name them

	void checknumber(int); 

In the call, you need to pass required arguments to the function

	checknumber(onetoten);
	checknumber(anotheronetoten);

In the definition, you need to describe the arguments and name them:

	void checknumber (int thenumber) 
	{	
		if (thenumber>=1 && thenumber<=10) {
			cout << "great, you can follow instructions" << endl;
		} else {
			cout << "You're an Idiot!" << endl;
		}
	}

It’s important to be clear about why I didn’t use the same name for the argument in the calls that I did in the definition. In the definition I used thenumber; In the first call, I used onetoten and in the second I used anotheronetoten. It would suck if I had to match the names of the arguments in the calls to the names of the arguments in the procedure, because I’d have to use the same variable each time I called the procedure. I wouldn’t be able to use onetoten the first time and anotheronetoten the second time. Since the argument names in the call don’t have to match the argument names in the procedure, I can call the procedure with different variables each time. I can also do this, if I wanted to:

	checknumber(4);
	checknumber(15);

That’s all for tonight. This weekend I’ll write something about the difference between functions and procedures, about using functions as values, and about passing by reference or by value.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: