bg287

csc110: Functions and Procedures Part II

Posted in csc110 by bnmng on 2010 03/26

In C++, there’s no formal difference between a procedure and a function, and most programmers will use the term “function” to describe either one. But when “procedure” is used, it is used to describe a function that doesn’t return a value. Procedures are defined as void, as in:

	void displayvalueof( int somevalue )
	{
		cout << "The value of something is: " << somevalue << endl;
	}

The procedure above can be called as follows:

	cout << "Enter a value: ";
	cin >> usersvalue;
	cout << endl;
	displayvalueof (usersvalue);

Functions often return a value. When they do, they are not called “procedures”, and they are defined by the type of value they return.

	int doublevalueof( int somevalue )
	{
		somevalue=somevalue*2;
		return somevalue;
	}

The function above doubles the value that it was given, then returns it. The line

	return somevalue;

, tells the function to pass the value of somevalue back to the caller.

When you call a function that returns a value, the function can be used as if it was that value.
The above function can be called as follows:

	cout << "Enter a value: ";
	cin >> usersvalue;
	doubledvalue=doublevalueof (usersvalue);
	cout << "The doubled value is: " << doubledvalue << endl;

The line
doubledvalue=doublevalueof (usersvalue);
assigns the value that the function returns to doubledvalue.

Note that I didn’t put a cout statement in the function. I could have done so, but generally it’s better to have your functions and procedures stay on mission. What I mean is, doublevalueof‘s mission is to double the value of something, displayvalueof‘s mission is to display the value of something. I could easily have written something that does both, but as a general rule, give your functions and procedures a mission and let them stay on that mission. This is the problem that our instructor has with the book. He feels that some of the functions in the book do too many different things.

The return type of the function and the types of its parameters don’t have to match. The function in the following program takes integer parameters but returns a float:

#include 
#include 

using namespace std;

float divide (int, int);

int main () {
	int firstnumber = 0, secondnumber=1;
	float quotient;
	
	cout << "Enter two integers for division.  A zero in the second integer will trigger an exit ('cause you can't divide by zero): " ;
	cin >> firstnumber >> secondnumber;
	cout << setw(5) << setprecision(7);


	while (secondnumber != 0)
	{
		cout << endl;

		quotient=divide (firstnumber, secondnumber);
		cout << "The result is: " << quotient << endl;
		cout << "Enter two more integers.  A zero in the second integer will trigger an exit: " ;
	
		cin >> firstnumber >> secondnumber;
	}
			

	//optional for Windows: replace both cin.get() with one system("PAUSE").
	cin.get();
	cin.get();

	return 0;
}

float divide (int numerator, int denominator) 
{
	float quotient;
	quotient= (float (numerator) / float (denominator)) ;
	return quotient;
}
	

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: