EvilZone
Programming and Scripting => C - C++ => : Chef 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;
}
#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]
-
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...
-
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.
-
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/ (http://www.cplusplus.com/doc/tutorial/functions2/)
seems like a short but good tutorial on functions (and func prototypes)
-
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).
-
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.
//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]
-
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...
-
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.