This forum is in archive mode. You will not be able to post new content.

Author Topic: help in user-defined function C++  (Read 3575 times)

0 Members and 1 Guest are viewing this topic.

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
help in user-defined function C++
« on: April 03, 2011, 03:38:10 AM »
hey guys Im just reading a book and Im making the part of the questions.
the question is the next:

===============================================================

3. Write a C++ program that uses three user-defined functions (counting main() as one)
and produces the following output:

Three blind mice
Three blind mice
See how they run
See how they run

One function, called two times, should produce the first two lines, and the remaining
function, also called twice, should produce the remaining output.

===============================================================

the definition of user-defined funtion from the book is:

Using a User-Defined Function That Has a Return
Value


Let’s go one step further and write a function that uses the return statement. The main() function already illustrates the plan for a function with a return value: Give the return type in the function header and use return at the end of the function body. You can use this form to solve a weighty problem for those visiting the United Kingdom. In the United Kingdom, many bathroom scales are calibrated in stone instead of in U.S. pounds or international kilograms. The word stone is both singular and plural in this context. (The English language does lack the internal consistency of, say, C++.) One stone is 14 pounds, and the program in Listing 2.6 uses a function to make this conversion.
Quote
// convert.cpp -- converts stone to pounds
#include <iostream>
int stonetolb(int); // function prototype
int main()
{
using namespace std;
int stone;
cout << “Enter the weight in stone: “;
cin >> stone;
int pounds = stonetolb(stone);
cout << stone << “ stone = “;
cout << pounds << “ pounds.” << endl;
return 0;
}
int stonetolb(int sts)
{
return 14 * sts;
}

Here’s a sample run of the program in Listing 2.6:

Enter the weight in stone: 14
14 stone = 196 pounds.


In main(), the program uses cin to provide a value for the integer variable stone. This value is passed to the stonetolb() function as an argument and is assigned to the variable sts in that function. stonetolb() then uses the return keyword to return the value of 14 * sts to main(). This illustrates that you aren’t limited to following return with a simple number. Here, by using a more complex expression, you avoid the bother of having to create a new variable to which to assign the value before returning it. The program calculates the value of that expression (196 in this example) and returns the resulting value. If returning the value of an expression bothers you, you can take the longer route:
Quote
int stonetolb(int sts)
{
int pounds = 14 * sts;
return pounds;
}

Both versions produce the same result, but the second version takes slightly longer to do so. In general, you can use a function with a return value wherever you would use a simple constant of the same type. For example, stonetolb() returns a type int value. This means you can use the function in the following ways:

Quote
int aunt = stonetolb(20);
int aunts = aunt + stonetolb(10);
cout << “Ferdie weighs “ << stonetolb(16) << “ pounds.” << endl;

In each case, the program calculates the return value and then uses that number in these statements. As these examples show, the function prototype describes the function interface—that is, how the function interacts with the rest of the program. The argument list shows what sort of information goes into the function, and the function type shows the type of value returned. Programmers sometimes describe functions as black boxes (a term from electronics) specified by the flow of information into and out of them. The function prototype perfectly portrays that point of view. (See Figure 2.9.)

http://img689.imageshack.us/i/dibujobcd.png/

FIGURE 2.9
The function prototype
and the function as a
black box.

The stonetolb() function is short and simple, yet it embodies a full range of functional features:
• It has a header and a body.
• It accepts an argument.
• It returns a value.
• It requires a prototype.
Consider stonetolb() as a standard form for function design. You’ll further explore functions
in Chapters 7 and 8. In the meantime, the material in this chapter should give you a good feel
for how functions work and how they fit into C++.

===============================================================

so how would I code it? plz someone help me and add slash for commentaries into the source code explaining how it works if its possible. thanx in advance

award: +2 of karma
« Last Edit: April 04, 2011, 06:58:45 AM by gh0st »

Offline Satan911

  • VIP
  • Knight
  • *
  • Posts: 289
  • Cookies: 25
  • Retired god/admin
    • View Profile
Re: help in C++
« Reply #1 on: April 04, 2011, 01:49:53 AM »
Quite easy. I didn't read everything you posted but it can be done like this: http://pastebin.com/LGK7xFfD
Satan911
Evilzone Network Administrator

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: help in C++
« Reply #2 on: April 04, 2011, 05:40:03 AM »
yeah it works thank you +2 of karma   ;)

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.