Can i create variables i can use anywhere? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can i create variables i can use anywhere?

I was wondering if I can create a variable inside a function, and be able to acces it outside its function. It's just a curiosity of mine.

12th Dec 2019, 7:08 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar
3 Answers
+ 2
- "Can i create variables i can use anywhere?" If the function returns something then its caller *should* be able to use whatever the function returns. Rather than trying to access a function's local variable from the outside (which breaks the variable scoping definition), I think you can opt to use the function's return value. - "Visual studio - C# - WFA" Not really sure about WFA, but logically you check whether the object's right offset had reached the border. When this condition evaluates to true, you reverse move direction. How to alter the move direction? you can have a variable (e.g. boolean 'MoveDir'), you invert its value as necessary, and this value should decide whether the object's horizontal point (left offset) is increased/decreased, within that timer event handler.
12th Dec 2019, 8:24 AM
Ipang
+ 2
After each time inteval wiil be adds some to for exaple x-position and check if x is grather than your Xmax. If yes than the x-position will be each time reduced up to the position Xmin etc.
12th Dec 2019, 9:42 AM
JaScript
JaScript - avatar
+ 1
Visual studio - C# - WFA I saw my teacher has a code, where he creates shapes (using some function) moving from the left side of the screen to the right side of the screen, and in contact with the border, it changes it's direction. Everything works on timers, he said. Do you have any idea how could that be done? (I just need some oppinions)
12th Dec 2019, 7:10 AM
🍇 Alex Tușinean 💜
🍇 Alex Tușinean 💜 - avatar