How to save function output as a variable?

I’m new to Julia and practicing writing functions…

The function below works great, if I call it from the command line like:

julia> prime(5)
[1, 2, 3, 5]

BUT if I try to set the output of the function to a variable, then it doesn’t work

julia> c = prime(5)
[1, 2, 3, 5]
julia> print(c)
nothing

What gives? See the prime function code below.

function prime(n::Integer)
    # Find all prime numbers between 1 and n
    num_all = collect(1:n);
    is_prime = trues(n)
    for i in num_all
        d_iv = collect(2:i-1)
        for ii in d_iv
            if i%ii == 0
                is_prime[i] = false
                continue
            end
        end
    end
    c = num_all[is_prime]
    return c
end

I tried your example and print(c) works as expected.

How do you run your code (which editor, Julia version, OS)?

Code was written in VS Code
Julia version 1.7.3
Windows 10

Ok, I restarted Julia and now it works fine. D’oh!

Thanks for the quick reply, @SteffenPL

1 Like