Rookie problem with variables
BlitzPlus Forums/BlitzPlus Programming/Rookie problem with variables
| ||
Since the help of B+ is pretty worse I can't find a solution to this problem. How can i exchange a variablele to the main loop of my program and a function Function test(a,b) Print b a=52 End Function ; main loop b=44 test(a,b) Print a While Not KeyHit(1) Wend End ---------------------------------- it returns 44 0 I don't get it. |
| ||
Because variables are local to the function they are in. If you pass a variable into a function, you aren't modifying the original - you're modifying the copy that was made for the function when it was called. Types are an exception to this rule, but that's another show... To do what you want, either declare "a" and "b" as global vars at the top of your program, or return "a" from the function and capture that return value in your main loop. |
| ||
Actully WarrenM a and b must be both variable so they cant' be globals How can I return a variable out of a function into the main loop? I cannot make a global of it inside a function. Why?!?!? Is it that hard to program such "feature" in B+ ? I can't see the bennefits of locking a variable in a function Anyway. see my goal below. When i click my mouse the function checkmousepos() is called In the function checkmousepos() is a calculation going on that returns the values tileborderx and tilebordery to the main loop. IN the mainloop the values tileborderx and tilebordery are used to draw a rectangle. Any clear advice? You was talking about capturing. Can you give me an example of that? Thank you |
| ||
Capturing:a = test(a,b) Then change "test" to read: Function test(a,b) Print b a=52 return a End Function Variable scoping is a very common (and necessary) thing in programming languages. If this is your first attempt at coding something, I understand your confusion. But in time you'll see that the reasons for it are very sound. If you want to be able to access a variable from anywhere in your program, declare it global at the top of your file. global MyVar Now, everyone can talk about MyVar and they'll be talking about the same variable. Blitz has the added confusion inherent to most BASIC dialects ... if you use a variable name that it hasn't seen in the current scope, it creates it for you automatically. This leads to all sort of fun programming errors related to scope confusion and name mismatches. But if you want to use BASIC, that's the trade off you have to accept. |
| ||
Great... but is it possible to return more then one varaibele out of a function since after a return the remaning content of the function will be ignored? I'll stop nagging after that :) |
| ||
No, you can't return multiple variables unless you wrap them in a Type. And if you're going to do that, just pass in the Type to the function since you ARE modifying the actual variable when it comes to Types. |
| ||
That is no problem. I have the tick to do as much as possible in functions now I own Visual Blitz. With the expand/collapse option) my code looks very clean. But I found out that a good organized mainloop is just as effective and more flexible. |