Function that returns 2 BigInts

I am having problems with global variables not being visible inside functions also within the main code. So I would like to return 2 BigInts from a function but I dont know how. When I try x = randomfunction() then I get the error “ERROR: LoadError: MethodError: Cannot convert an object of type Vector{BigInt} to an object of type BigInt”

Do you have x::BigInt somewhere above? What you’re describing should work just fine

function foo()
    # two BigInts:
    10000000000000000000000, 10000000000000000000000
end 

#######

julia> x = foo()
(10000000000000000000000, 10000000000000000000000)

So that returns a tuple…so then I can access it by x[1] and x[2]. I thought I tried this but I will give it another shot tomorrow. Do I define x before assigning it the returned tuple? Should I define x = (0,0) so it knows x should take a tuple? I tried defining it as an array or a vector I think…yeah with ::BigInt…but need to see tomorrow.

So that returns a tuple…so then I can access it by x[1] and x[2].

a, b = foo()

is also fine.

Do I define x before assigning it the returned tuple? Should I define x = (0,0) so it knows x should take a tuple?

No need. What you saw above were the only two things I typed in a fresh julia session; nothing else is necessary.

I tried defining it as an array or a vector I think…yeah with ::BigInt…but need to see tomorrow.

A vector is also fine

julia> function foo()
           [big(i) for i in 1:5]
       end
foo (generic function with 1 method)

julia> x = foo()
5-element Vector{BigInt}:
 1
 2
 3
 4
 5

Yeah I tried the a, b thing but maybe because a and b were defined as bigints previously it didnt work

Why don’t you download Julia onto the device you’re using, so you can play with it right now? It’s free, you know.

Ok reproduced the error…the problem is b and c can’t previously be defined. Thanks for the help.

begin
function a()
return BigInt(2),BigInt(3)
end
b::BigInt
b,c=a()
println(typeof(b))
end

Can you show some code which illustrates what you mean by this? Perhaps you and I have different ideas of what this means.

b and c are “previously defined” here:

julia> b, c = "hello", "world!"
("hello", "world!")

julia> c, b
("world!", "hello")

julia> function a()
           return BigInt(2), BigInt(3)
       end
a (generic function with 1 method)

julia> b, c = a()
(2, 3)

julia> b, c, b, c, b, c
(2, 3, 2, 3, 2, 3)

If you run my code in replit it will not work because of b::BigInt

Ah, I see. Yes, in Julia, you cannot simply declare the type of a variable like that. That’s because this syntax is already used for type assertion.

You can, however, declare the type of a variable when assigning it:

julia> function a()
           return BigInt(2), BigInt(3)
       end
a (generic function with 1 method)

julia> b::BigInt, c::BigInt = a()
(2, 3)

To illustrate how the type assertion works:

julia> b::BigInt
2

julia> b::Int
ERROR: TypeError: in typeassert, expected Int64, got a value of type BigInt

Alternatively, you can declare unassigned variables by specifying their scope (with global, local, or let keywords), and then you can also declare their type:

julia> global d::BigInt

julia> d = 5
5

julia> typeof(d)
BigInt

julia> d = "hi"
ERROR: MethodError: Cannot `convert` an object of type String to an object of type BigInt

Please try it out so You can see the error it’s the same as My Main code

I am trying to share My code but discourse converts : / to a sade face…