call variables defined in different jl file

229 views
Skip to first unread message

new to Julia

unread,
Mar 23, 2016, 10:40:35 AM3/23/16
to julia-users
Hi all:

I have two function files saved in two different jl file. I can call files. However, for variables defined in one file, I cannot use it in the other file. The following is a simple example:

function test(x)
  y1=6*x;
  y2=x/5;
  y1,y2
end
pwd()



## test and test2 are used for calling functions in Julia
function test2(x)
  include("test.jl")
  yold,ynew=test(x/c);
  y3=yold+10;
  y4=ynew-10;
  yold2,ynew2=test(x)
  y5=yold2+20;
  y6=ynew2-20;
  y3,y4,y5,y6
end
y3,y4,y5,y6=test2(100)

when I run test2, it always comes an error: c is not defined.

I am wondering that how to do with this? Thank you!

Yichao Yu

unread,
Mar 23, 2016, 10:44:04 AM3/23/16
to Julia Users
include works in global scope. You can't do this.

Isaiah Norton

unread,
Mar 23, 2016, 10:45:47 AM3/23/16
to julia...@googlegroups.com
- include should be used outside the function.
- 'c' is not defined anywhere in your code.

```
f1 inorton$ cat foo.jl
c = 100
f1 inorton$ cat bar.jl
include("foo.jl")

println("c = ", c)
f1 inorton$ julia bar.jl
c = 100
```


On Tue, Mar 22, 2016 at 8:02 PM, new to Julia <weini...@gmail.com> wrote:

Isaiah Norton

unread,
Mar 23, 2016, 10:48:19 AM3/23/16
to julia...@googlegroups.com
Also, the following section of the manual may be helpful:

Reply all
Reply to author
Forward
0 new messages