Hello everyone,
I am currently working on a script in which the same function “g” has to be evaluated multiple times, also within other functions.
1. a=3
2. function g(y)
3. a*y
4. end
5. function f(x,y)
6. function g(y)
7. a*y
8. end
9. a=4
10. x+g(y)
11. end
12. println(g(3))
13. println(f(1,3))
This function “g” depends on both the argument “y” passed to the function itself and a certain parameter “a”. The script above does exactly what I want, indeed, the command println(g(3)) returns value 9, evaluated adopting parameter a=3 defined in the Main module, while println(f(1,3)) returns 13, meaning that when “g” is called within function “f”, this is evaluated with the value of a=4 defined in the scope of function “f”.
In the script on which I am working, “g” appears many times and every time I want it to be evaluated with the parameter “a” defined in the scope of the caller, without passing “a” as an argument to the function.
Since “g” has always the same form I would like to import it from an external file “g.jl” in order to make modifications to the form of “g” easier to implement. The script may appear as follows:
file g.jl:
1. function g(y)
2. a*y
3. end
file myscript.jl:
1. a=3
2. include("g.jl")
3. function f(x,y)
4. include("g.jl")
5. a=4
6. x+g(y)
7. end
8. println(g(3))
9. println(f(1,3))
However now, the command println(f(1,3)) returns 10 and not 13, meaning that when “g” is called, that is evaluated adopting a=3 from the main. So my question is: is there any way of including a script present in an external file executing it only in the scope from which it is called? Hope my question is clear. Thanks to anybody willing to help.