|
-
Jan 5th, 2003, 05:10 PM
#1
Thread Starter
Frenzied Member
Substitution of variables in functions?
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;
}
-
Jan 5th, 2003, 07:16 PM
#2
Yes. But only the value. If you change s in cube then side won't change because s is a copy of side.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 07:47 PM
#3
Thread Starter
Frenzied Member
But cube s will return the value of side*side*side, right.
-
Jan 5th, 2003, 08:18 PM
#4
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 5th, 2003, 08:44 PM
#5
Thread Starter
Frenzied Member
Is the way that is set up uncommon? Is there a better way to do it?
-
Jan 5th, 2003, 09:49 PM
#6
Hyperactive Member
That looks normal to me. Unless you want to pass by reference:
Code:
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;
}
but that is totally bizzare in this example
-
Jan 5th, 2003, 10:35 PM
#7
Thread Starter
Frenzied Member
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.
-
Jan 6th, 2003, 02:31 AM
#8
Guru
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;
}
-
Jan 6th, 2003, 02:34 AM
#9
Guru
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:
Code:
void G(int *x)
{
printf("G: %d\n", *x);
printf("G: Changing to 42...\n");
*x = 42;
}
And the call from main:
Code:
b = 2;
printf("main: %d\n", b);
G(&b);
printf("main: %d\n", b);
References have a simpler syntax, so use them if you can.
-
Jan 6th, 2003, 05:43 AM
#10
Hyperactive Member
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!
-
Jan 6th, 2003, 10:07 AM
#11
Guru
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.)
-
Jan 6th, 2003, 10:35 AM
#12
Monday Morning Lunatic
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) {
// ...
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 6th, 2003, 01:12 PM
#13
Guru
In which you can also, of course, unconventionally cheat:
Code:
func(*static_cast<large_struct*>(0)); // :rolleyes:
-
Jan 6th, 2003, 01:18 PM
#14
Monday Morning Lunatic
Indeed. But in doing that, all bets are off, and most compilers will pick up on the attempt to dereference 0 anyway.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 6th, 2003, 01:28 PM
#15
Originally posted by Yonatan
In which you can also, of course, unconventionally cheat:
Code:
func(*static_cast<large_struct*>(0)); // :rolleyes:
That's still the problem of pointers, not references.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 6th, 2003, 01:43 PM
#16
Thread Starter
Frenzied Member
If I ever get Dev C++ to compile anything I will learn more and be able to figure these things out.
-
Jan 6th, 2003, 01:56 PM
#17
Frenzied Member
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...)
Jop - validweb.nl
Alcohol doesn't solve any problems, but then again, neither does milk.
-
Jan 6th, 2003, 02:08 PM
#18
Thread Starter
Frenzied Member
I posed a new thread for it. Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|