Assigning a value to a structure element that figures as argument in a function
-
I'm not sure if that title makes sense, but I'll try to explain.
a = [.b = 0] function c(d) d = 1 return void c(a.b)
After running this function, the value of a.b is still 0. It seems I can't change the value of a.b to 1 by feeding it into a function that changes the value of its argument to 1. Is such a function impossible? Can someone explain why and tell me if there is another way of doing this?
-
you are passing the value not the refence, you could do a.b = c(a.b)
-
@MikeDX I'm afraid I don't understand the terminology. What does it mean to pass a reference?
-
@MikeDX I think I get it now. What goes into the function is the value of a.b, whatever that is.
-
that's right. you are sending it the value and not the variable
-
But if you declare an array of the structure, I strongly believe you would pass a reference (that's how array is usually implemented). Here is the code I use:
{code}
struct sometype
float elevation
...
endStructsometype terrainHeightMap[32][32]
function setup(terrainMap)
for x=0 to 32 loop
for y=0 to 32 loop
terrainMap[x][y].elevation = ... // whatever you want here
repeat
repeat
return void// Later on:
print(terrainHeightMap[10][10].elevation)
{code} -
that is true but he has passed a.b and not just b 👍
-
Yes, exactly, that was my 2 cents on how to assign a value into structure, when needed. Not sure what happens if the whole structure is passed as an argument, if that is a reference or a value. Mu guess is by value ;-). I.e in this what would happen if only a is passed and the function would assign a.b=3