void function issue

Oct 17, 2016 at 10:17pm
I have an issue while assigning values in a void function..
Outputs = 0, 0;
1
2
3
4
5
6
void AssignValue( sdl_rect rect, int xx, int yy)
{
  Rect.x = xx;
  Rect.y = yy;

}

And
1
2
3
4
5
6

...
AssignValue(dstrect, 50, 100);
cout << dstrect.x << endl;
cout << dstrect.y << endl;
...

Output = 0..
Can you help me? I need it.. thanks!
Oct 17, 2016 at 10:58pm
I'm not sure this is what you want but shows an example how to get it to print.



1
2
3
4
5
6
7
8
9
10
11
12
void AssignValue( int xx, int yy)
{
  cout << xx;
  cout << ",";
  cout << yy;
}

int main()
{
AssignValue(50, 100);
return 0;
}
Oct 18, 2016 at 6:02am
No.. i don't want to print anything..
I wrote "cout" only for testing the results..
I'll explain:
There's a void function.. than function assign value to a Rect of type Sdl_rect ( i am using SDL2 now )..
If i assign value to the rect by "AssignValue(dstrect, 50, 100), dstrect doesn't change value like i want.. members of this are x,y,w,h.. they keep 0.
The void function is above.. please i need help.. i always did the same with free free pascal and it always worked for me.. please
Oct 18, 2016 at 6:57am
void function does not return values
Oct 18, 2016 at 7:41am
closed account (1vRz3TCk)
Manuel Di Criscito, read up on passing by reference.
Oct 18, 2016 at 8:24am
Yes, please note CodeMonkey's post. It WILL change values if you make the parameter a reference, using &.

Thus:
void AssignValue( sdl_rect &Rect, int xx, int yy)

(Note that I have added & ... but I have also changed your original rect (lower case r) to Rect (upper case R) - I'm surprised that your code as written actually compiled.)

You may want to read up on 'constructors' too, as this seems to be what AssignValue is actually doing and it would be quite natural here. Also, make sure that members x and y of your class or structure sdl_rect are public and not private.
Last edited on Oct 18, 2016 at 8:35am
Oct 18, 2016 at 12:03pm
Oh thanks, i'll try.
I was writing this topic on the phone.. too possible for presence of errors but syntax is correct i think..
What happens adding &??
I am still learning c++ and coming out from object-pascal programming ( always with SDL2 lib).
I downloaded an app "Learn C++" by SoloLearn .
I am italian but i still learn c++ from that app!
So.. what happens adding & ? I should see
Topic archived. No new replies allowed.