Global Variable / Local Variable crossing

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

User avatar
Rudi
Posts: 666
Joined: Mon Feb 10, 2014 4:59 am
Has thanked: 494 times
Been thanked: 188 times

Global Variable / Local Variable crossing

Postby Rudi » Fri Mar 28, 2014 7:25 pm

Hi

only a question:
is it possible example

in main program to catch a local variable from a macro like this

flowchart
main...calc....calc2

in main
mainglobal=calc.local + calc2.local


and / or

is it possible in a macro to catch a localvariable from main / macro like

calclocal = main.local + calc2.local

Thank you!

Rudi
;-)

Kenrix2
Flowcode v5 User
Posts: 211
Joined: Tue Feb 19, 2013 9:51 pm
Has thanked: 72 times
Been thanked: 177 times
Contact:

Re: Global Variable / Local Variable crossing

Postby Kenrix2 » Sat Mar 29, 2014 12:12 am

Using global variables would make it very simple. Using local variables is possible, but I don't understand the advantage. Attached is a program using only local variables. I am still learning V6 so maybe an experienced V6 programmer can come up with a better way.
Attachments
pass a local variable.fcfx
(4.56 KiB) Downloaded 82 times
These users thanked the author Kenrix2 for the post:
Rudi (Sat Mar 29, 2014 3:01 pm)
Rating: 5.26%
 

kersing
Valued Contributor
Valued Contributor
Posts: 1873
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 530 times
Been thanked: 1040 times
Contact:

Re: Global Variable / Local Variable crossing

Postby kersing » Sat Mar 29, 2014 12:35 am

Rudi wrote:in main program to catch a local variable from a macro like this

flowchart
main...calc....calc2

in main
mainglobal=calc.local + calc2.local


No, this is not possible. After you leave a macro the local variables are destroyed and the values are lost.

Rudi wrote:is it possible in a macro to catch a localvariable from main / macro like

calclocal = main.local + calc2.local


If you need access to a variable from multiple macros (main and another macro):
1) If you do not need to modify the variable, use it as an argument to the macro call. That way you know it is being used in the macro.
2) If only one variable needs to be modified, return the value from the macro and assign it in the calling macro.
3) If you need to modify multiple variables, use global variables. It is good programming practice to avoid using global variables and assigning them values in macros as much as possible as it is hard to trace where and when global variables are modified.

Examples of the points above:

call.JPG
call.JPG (14.12 KiB) Viewed 2370 times


"glob1" and "glob2" are examples of 1 in the list above. ".loc1" is example of 2.

example.JPG
example.JPG (13.02 KiB) Viewed 2370 times


"glob4" and "glob5" are examples of 3.
These users thanked the author kersing for the post (total 2):
Rudi (Sat Mar 29, 2014 3:00 pm) • LeighM (Sat Mar 29, 2014 6:41 pm)
Rating: 10.53%
 
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis