Author Topic: [C++] user defined function w/ returning value  (Read 2699 times)

0 Members and 1 Guest are viewing this topic.

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
[C++] user defined function w/ returning value
« on: July 08, 2013, 10:19:56 pm »
I'm reading this book, Cpp Primer Plus 6th Edition by Stephen Prata. I'm getting into User-defined functions with return values. The code it is using as an example has some parts which I don't quite understand.
I don't understand the function prototype global declaration:
I said "global". Can function prototype's be local? Or are they always global?
int stonetolb(int);
 
The next part I don't understand is at the very end of the code. What is "sts" and when is it supposed to be used?
int stonetolb(int sts) 
{
return 14 * sts;
}
Code: [Select]
#include <iostream>
 
#include <conio.h>
 
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;
   
_getch();
  return 0;
   
}
  int stonetolb(int sts)
   
{
  return 14 * sts;
   
}[code] 
 
"To find happiness is to not always laugh."

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [C++] user defined function w/ returning value
« Reply #1 on: July 08, 2013, 10:27:24 pm »
Maybe they mean with "global", that the function doesn't belong to a class, so you can call it "global", "foo()" instead of "baz.foo()" (baz is the name of the class) (or however it is done in C++)


"sts" is the name of the variable that is used to temporary store the value that is passed on when calling the function ( stonetolb(stone) ). So "sts" contains the value of "stone" in your example. You then calculate the new weight (14 * sts) and return it...
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: [C++] user defined function w/ returning value
« Reply #2 on: July 08, 2013, 10:31:05 pm »
Maybe they mean with "global", that the function doesn't belong to a class, so you can call it "global", "foo()" instead of "baz.foo()" (baz is the name of the class) (or however it is done in C++)


"sts" is the name of the variable that is used to temporary store the value that is passed on when calling the function ( stonetolb(stone) ). So "sts" contains the value of "stone" in your example. You then calculate the new weight (14 * sts) and return it...

To name the class in C++ "baz" it would be "baz foo()" (No period or parentheses)
What language do you know man? Thanks for answering even though I think you know a language other than C++.

Would you, or anyone reading this, happen to know of a tutorial on function prototypes?
They seem extremely commonplace in C++ so I wanna get a grasp on it.
« Last Edit: July 08, 2013, 10:31:36 pm by Chef »
"To find happiness is to not always laugh."

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [C++] user defined function w/ returning value
« Reply #3 on: July 08, 2013, 10:37:22 pm »
What language do you know man? Thanks for answering even though I think you know a language other than C++.
C, Python, Java, PHP

Would you, or anyone reading this, happen to know of a tutorial on function prototypes?
They seem extremely commonplace in C++ so I wanna get a grasp on it.

http://www.cplusplus.com/doc/tutorial/functions2/
seems like a short but good tutorial on functions (and func prototypes)
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline dense

  • Serf
  • *
  • Posts: 23
  • Cookies: 4
    • View Profile
Re: [C++] user defined function w/ returning value
« Reply #4 on: July 08, 2013, 10:53:39 pm »
Global means it's accessible everywhere, in every scope.
So locals are the ones you can't access everywhere like functions in a class (the ones that aren't static).
« Last Edit: July 08, 2013, 10:55:37 pm by dense »

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: [C++] user defined function w/ returning value
« Reply #5 on: July 08, 2013, 11:04:16 pm »
it is, but isn't every function global, if you say it that way?

So I thought, maybe they mean "globally declared", something like declared outside of any class...
That's what I was trying to say/ask. Basically every function prototype that is not in a class is "global" in a sense.
 
 
Okay so I have finally come to the point where I see code and I think of a different way to write it. There is this code in the book that I write in a different way - a way that I am comfortable with.
Let me know the advantages/disadvantages of what I wrote and what is in the book. Personally I like my way a lot more because it's more simple and a lot easier for me to understand.

Code: [Select]
//MY CODE
#include <iostream>
 
#include <conio.h>
 
int random(int);
 
int main(void)
   
{
     int stone;
 
   cout << "Enter the weight in stone: ";
   
   cin >> stone;
     cout << "That weight in stone, in pounds is " << stone*14 << ".\n";
 
   cout << "Thank you and have a nice day.";
   
   _getch();
 
}
 
//THE CODE IN THE BOOK IS BELOW

#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;
 

[code] 
 
« Last Edit: July 08, 2013, 11:06:57 pm by Chef »
"To find happiness is to not always laugh."

Offline flowjob

  • Knight
  • **
  • Posts: 327
  • Cookies: 46
  • Pastafarian
    • View Profile
Re: [C++] user defined function w/ returning value
« Reply #6 on: July 08, 2013, 11:13:07 pm »
Let me know the advantages/disadvantages of what I wrote and what is in the book. Personally I like my way a lot more because it's more simple and a lot easier for me to understand


Your example works aswell, but I guess the books example is about teaching functions, so it wouldn't make any sense doing it without a function. Also, this is just a simplified example. Normally you wouldn't declare a function just for one easy calculation, but when you got several repeating lines of code it would make sense to wrap them in a function instead. Makes troubleshooting a lot easier...
« Last Edit: July 08, 2013, 11:18:36 pm by flowjob »
Quote
<phil> I'm gonna DDOS the washing machine with clothes packets.
<deviant_sheep> dont use too much soap or youll cause a bubble overflow

Offline Chef

  • Peasant
  • *
  • Posts: 126
  • Cookies: 3
  • Corrupted Soul
    • View Profile
Re: [C++] user defined function w/ returning value
« Reply #7 on: July 08, 2013, 11:18:03 pm »

Your example works aswell, but I guess the books example is to teach functions, so it wouldn't make any sense doing it without a function. Also, this is just a simplified example. Normally you wouldn't declare a function just for one easy calculation, but when you got several repeating lines of code it would make sense to wrap them in a function instead. Makes troubleshooting a lot easier...

LOL I just noticed I wrote a function prototype but didn't even use it. I can see how function prototype's will be needed when codes get larger and more complex. Well, I'll get back to the books. I'll get on IRC for sure - in about 2 hours.
"To find happiness is to not always laugh."