# Function that returns 2 BigInts

**URL:** <https://discourse.julialang.org/t/function-that-returns-2-bigints/93968>\
**Category:** New to Julia\
**Created:** [February 3, 2023, 2:21am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968 "2023-02-03T02:21:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 3, 2023, 2:21am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/1 "2023-02-03T02:21:08Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [February 3, 2023, 3:21am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/2 "2023-02-03T03:21:47Z")

</div>

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

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

#######

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

```

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 3, 2023, 3:36am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/3 "2023-02-03T03:36:09Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)\
**Post date:** [February 3, 2023, 3:45am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/4 "2023-02-03T03:45:27Z")

</div>

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

```julia
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
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

```

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 3, 2023, 3:56am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/5 "2023-02-03T03:56:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 3, 2023, 4:02am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/6 "2023-02-03T04:02:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 3, 2023, 4:23am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/7 "2023-02-03T04:23:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 3, 2023, 5:06am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/8 "2023-02-03T05:06:14Z")

</div>

> [@x13420x](#):
>
> the problem is b and c can’t previously be defined.

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
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)

```

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 3, 2023, 5:09am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/9 "2023-02-03T05:09:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 3, 2023, 5:17am UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/10 "2023-02-03T05:17:05Z")

</div>

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
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
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
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

```

---

<div class="post-metadata">

**Author:** ![x13420x](https://avatars.discourse-cdn.com/v4/letter/x/ecc23a/32.png) [@x13420x](https://discourse.julialang.org/u/x13420x)\
**Post date:** [February 3, 2023, 12:00pm UTC](https://discourse.julialang.org/t/function-that-returns-2-bigints/93968/11 "2023-02-03T12:00:37Z")

</div>

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

> **[Replit: the collaborative browser based IDE](https://replit.com#&92%40x13420x/Return-2-bigints?s=app)**
>
> Run code live in your browser. Write and run code in 50+ languages online with Replit, a powerful IDE, compiler, & interpreter.

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