calling files in Julia

1,953 views
Skip to first unread message

new to Julia

unread,
Mar 23, 2016, 2:41:29 PM3/23/16
to julia-users
I have a question for calling files in Julia:

I have two jl files. And I can call one file from the other file. However, the variable or constant defined in that file cannot be used in the other file. I am wondering that how to fix this? The following is a simple example.

function test(x)
  c=2;
  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)


However, when I run this test2, there is a error comes out: saying that c is not defined.

Thank you very much.

Christopher Alexander

unread,
Mar 23, 2016, 2:57:16 PM3/23/16
to julia-users
How is test2 supposed to know what "c" is?  It is only defined inside the scope of the function "test", so it won't be accessible anywhere else.

Chris

Isaiah Norton

unread,
Mar 23, 2016, 3:15:59 PM3/23/16
to julia...@googlegroups.com

new to Julia

unread,
Mar 23, 2016, 3:39:10 PM3/23/16
to julia-users
Thanks for your reply. Does it mean that calling files in Julia is impossible?

Christopher Alexander

unread,
Mar 23, 2016, 3:48:40 PM3/23/16
to julia-users
No, you can call files using "include", but you are only going to import essentially the functions, types, and global variables defined in those files (you should not include a file inside of a function).  You do see though how your variable "c" in "test" would not be accessible anywhere else, right?

Chris

new to Julia

unread,
Mar 24, 2016, 12:42:24 AM3/24/16
to julia-users

Thank you so much for your reply. I am still not very clear about what to do. Could you explain to me again?

Uwe Fechner

unread,
Mar 24, 2016, 2:30:26 AM3/24/16
to julia-users
If c is a constant, that you want to define in the file test.jl, than you can define it e.g. at the top of the file
OUTSIDE of the function like this:
const c=2

new to Julia

unread,
Mar 24, 2016, 12:29:17 PM3/24/16
to julia-users
Got it. Thank you so much. I am wondering that what about variable? I may need to define it in the function, right? How to make it be called by other files?

Tim Holy

unread,
Mar 24, 2016, 2:34:08 PM3/24/16
to julia...@googlegroups.com
We usually talk about calling "functions" rather than "files." If you're coming
from a Matlab background, one thing to note about julia is that you can put
multiple functions in one file:

square_and_mult_by_c(x, c) = c*x*x

smc(X::AbstractVector, c) = [square_and_mult_by_c(x, c) for x in X]

Note the second function calls the first, but you can put both of these in the
same disk file. Once loaded, you can also call either of these functions from
the command line.

Functions can have more than one argument, so you can pass both `x` and `c`
from one function to another. You might also be interested in the manual
sections on default and keyword arguments.

Best,
--Tim

new to Julia

unread,
Mar 24, 2016, 2:42:54 PM3/24/16
to julia-users
Thank you very much for your reply. I am wondering that if I put two functions in two files (same as Matlab did), does it slow the coding speed? If not, how can I use variables already defined in one function?

Uwe Fechner

unread,
Mar 24, 2016, 3:22:49 PM3/24/16
to julia-users
Hello,

do not try to use variables, that you have defined in one function within another function.

I would suggest the following approach, if you need to modify variables in multiple functions:

```
# if you need to modify multiple variables in multiple functions,
# define a data type that holds all your variables
type Data
    a::Number
    b::Number
    c::Number
end

# declaring data as const has the main meaning, that the type stays the same,
# you CAN change the values of the fields later
const data = Data(1,2,3)

# pass an instance of the type Data to your function
function test1(dat, x, y)
    dat.a*x + dat.b*y
end

# to a second function, pass the same variable
function test2(dat, x1, y1)
    # you can change the value of the fields in the function
    dat.a += 1
    dat.b *= 2
    dat.a^x1 + dat.b^y1
end

x = 1.0
y = 3.0

println(test1(data, x, y))
println(data.a, " ", data.b)

println()
println(test2(data, x, y))
println(data.a, " ", data.b)
```

If you want to define your function in different files, that put the type definition in a third file and include
this file in both files, that define your functions.

Performance wise it should not make much of a difference, if you define many functions in one file or in
different files.

Uwe

Tim Holy

unread,
Mar 24, 2016, 6:27:17 PM3/24/16
to julia...@googlegroups.com, new to Julia
On Thursday, March 24, 2016 11:42:53 AM new to Julia wrote:
> Thank you very much for your reply. I am wondering that if I put two
> functions in two files (same as Matlab did), does it slow the coding speed?
> If not, how can I use variables already defined in one function?

You can't generally do this, with the obvious exception of passing them back
in the outputs of the function. This is what is called "scope." If I write one
function that sets i=1 internally, you don't want that "contaminating" the
notion of i for other functions. So the fact that julia doesn't allow what
you're asking is not a weakness in julia; it's considered good design, and
every language I know of (which is not very many :-) ) does it this way.
Reply all
Reply to author
Forward
0 new messages