Does cube(side) send the value of side to s at the top?
inline float cube(const float s) {return s*s*s;}
int main() {
float side; cin >> side;
cout << "Volume of cube with side" << side<< "is "
<< cube(side) << endl;
return 0;
}
Printable View
Does cube(side) send the value of side to s at the top?
inline float cube(const float s) {return s*s*s;}
int main() {
float side; cin >> side;
cout << "Volume of cube with side" << side<< "is "
<< cube(side) << endl;
return 0;
}
Yes. But only the value. If you change s in cube then side won't change because s is a copy of side.
But cube s will return the value of side*side*side, right.
Logically yes. Actually it will return the value of s*s*s, but since s has the same value as side the result is effectivly side*side*side.
Is the way that is set up uncommon? Is there a better way to do it?
That looks normal to me. Unless you want to pass by reference:
but that is totally bizzare in this exampleCode:inline void cube(float& s) {s = s * s * s;}
int main() {
float side; cin >> side;
float se = side;
cube(se);
cout << "Volume of cube with side" << side<< "is "
<< se << endl;
return 0;
}
Could you explain how pass by reference works there. I never saw much use in pass by reference but you did something a little different.
Passing by reference means a function can change the value of the variable. Here, it doesn't receive just a value, by an entire variable which it can write to.
Try this:Code:void F(int x)
{
cout << "F: " << x << endl;
cout << "F: Changing to 17..." << endl;
x = 17;
}
void G(int& x)
{
cout << "G: " << x << endl;
cout << "G: Changing to 42..." << endl;
x = 42;
}
void H(int& x)
{
cout << "H: Changing to 5..." << endl;
x = 5;
}
int main()
{
int a, b, c;
cout << endl;
// Test F
a = 1;
cout << "main: " << a << endl;
F(a);
cout << "main: " << a << endl;
cout << endl;
// Test G
b = 2;
cout << "main: " << b << endl;
G(b);
cout << "main: " << b << endl;
cout << endl;
// Test H
// Note that the variable c is NOT INITIALIZED
H(c);
cout << "main: " << c << endl;
return 0;
}
By the way, references are new to C++.
In C you have only pointers. In C++ you have both.
For an example of how pointers are written, the above function G in C:And the call from main:Code:void G(int *x)
{
printf("G: %d\n", *x);
printf("G: Changing to 42...\n");
*x = 42;
}
References have a simpler syntax, so use them if you can. :)Code:b = 2;
printf("main: %d\n", b);
G(&b);
printf("main: %d\n", b);
References have a disadvantage though. They can not be changed.
e.g.
Code:int z[20];
int* p;
int& e = z[1];
p = &z[1];
p = &z[2];
e = z[8]; //will not work!
Or an advantage, depending on how you look at it.
(If you look at it in a way that makes it seem like an advantage, you're looking at OOP.)
You can change a variable *through* any non-const reference to it. You cannot change the reference *itself* though once set. This is useful because, you avoid the following check::)Code:void func(large_struct *ptr) {
if(!ptr) return;
/* ... */
}
void func(large_struct &ref) {
// ...
}
In which you can also, of course, unconventionally cheat:
Code:func(*static_cast<large_struct*>(0)); // :rolleyes:
Indeed. But in doing that, all bets are off, and most compilers will pick up on the attempt to dereference 0 anyway.
That's still the problem of pointers, not references.Quote:
Originally posted by Yonatan
In which you can also, of course, unconventionally cheat:
Code:func(*static_cast<large_struct*>(0)); // :rolleyes:
If I ever get Dev C++ to compile anything I will learn more and be able to figure these things out.
Please post the errors you get, we may be able to help you out than.. are you sure all the (include) paths are set correctly? (just a guess...)
I posed a new thread for it. Thank you.