# Why does multiple return not work?

**URL:** <https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777>\
**Category:** New to Julia\
**Created:** [November 25, 2020, 9:03pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777 "2020-11-25T21:03:22Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 25, 2020, 9:03pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/1 "2020-11-25T21:03:22Z")

</div>

Hi all,

I am currently playing around with Julia coming from Python. So far I am really thrilled. But today I stumbled over a bug I don’t understand.

Given I am very new to Julia I copied the entire code in as I am unsure where the bug actually sits.

With the below code I intend to take a string formula and (eventually) use it for GLM model.

However, my function `parser` doesn’t return anything (only a dummy function so far). Can anyone help me and explain what is wrong with the return statement in the function `parser`

```julia
using DataFrame, CSV, GLM

function extract(formula::String)
	strY, strX = [strip(el) for el in split(formula, "~")]
	y = Symbol(strY)
	X = [Symbol(strip(el)) for el in split(strX, "+")]
	vcat(y,X)
end;

function parser(cs::Array{Symbol,1}, df::DataFrame)
	df[[cs[1]]], df[cs[2:end]]
end

df = CSV.read("Credit.csv")
cols = extract("Rating ~ Income + Education")
parser(cols, df) # Does not return anything

```

Thanks

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [November 25, 2020, 9:35pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/2 "2020-11-25T21:35:50Z")

</div>

I tried your code with a mock Dataframe and it appears to do what you want:

```julia
using DataFrames

function extract(formula::String)
	strY, strX = [strip(el) for el in split(formula, "~")]
	y = Symbol(strY)
	X = [Symbol(strip(el)) for el in split(strX, "+")]
	vcat(y,X)
end;

df = DataFrame(Rating = 1:4, Income = ["M", "F", "F", "M"], Education = 6:9)
4×3 DataFrame
│ Row │ Rating │ Income │ Education │
│ │ Int64 │ String │ Int64 │
├─────┼────────┼────────┼───────────┤
│ 1 │ 1 │ M │ 6 │
│ 2 │ 2 │ F │ 7 │
│ 3 │ 3 │ F │ 8 │
│ 4 │ 4 │ M │ 9 │

julia> function parser(cs::Array{Symbol,1}, df::DataFrame)
               df[[cs[1]]], df[cs[2:end]]
       end
parser (generic function with 1 method)

julia> cols = extract("Rating ~ Income + Education")
3-element Array{Symbol,1}:
 :Rating
 :Income
 :Education

julia> parser(cols, df)
(4×1 DataFrame
│ Row │ Rating │
│ │ Int64 │
├─────┼────────┤
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 3 │
│ 4 │ 4 │, 4×2 DataFrame
│ Row │ Income │ Education │
│ │ String │ Int64 │
├─────┼────────┼───────────┤
│ 1 │ M │ 6 │
│ 2 │ F │ 7 │
│ 3 │ F │ 8 │
│ 4 │ M │ 9 │)

```

One thing I noticed is that you are `using DataFrame` instead of `DataFrames` is that intentional? Do you see any error? what is the output of the 2 lines previous to your call to `parser`?

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [November 25, 2020, 9:43pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/3 "2020-11-25T21:43:35Z")

</div>

Couple of things, that aren’t answering your question, but maybe help you later.

1. `CSV.read` should error out or you are using very old version. It would make sense to upgrade it.
2. Functions are overspecialized. There is no need in doing it, unless you know exactly why.
3. You can get rid of list comprehensions in favor of broadcasting.
4. New version of `DataFrames` support String variables as a column name and do not support vector arguments.

So, your code can look like

```julia
using DataFrames, CSV, GLM

function extract(formula)
	strY, strX = strip.(split(formula, "~"))
	X = strip.(split(strX, "+"))
	vcat(y,X)
end;

function parser(cs, df)
	df[!, [cs[1]]], df[!, cs[2:end]]
end

df = CSV.read("Credit.csv", DataFrame)
cols = extract("Rating ~ Income + Education")
parser(cols, df)

```

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 25, 2020, 9:57pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/4 "2020-11-25T21:57:39Z")

</div>

Thanks to both of you for your help. Very helpful.

I believe I found the bug. Running the file as a `.jl` file works. Running the code in `Pluto` it returns an empty tuple. Any idea why this could be?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 25, 2020, 10:09pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/5 "2020-11-25T22:09:34Z")

</div>

What do you mean by `empty tuple`?

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 25, 2020, 10:14pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/6 "2020-11-25T22:14:23Z")

</div>

It just returns `( , )` .

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 25, 2020, 10:16pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/7 "2020-11-25T22:16:13Z")

</div>

What’s the type of the object? `typeof(parser(cols, df)`

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 25, 2020, 10:19pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/8 "2020-11-25T22:19:42Z")

</div>

I could have thought of that myself…thanks!

The type is `Tuple{DataFrame,DataFrame}`. It appears as if the output is just not properly formatted. If I use the output in a different cell the output is actually correct then.

```julia
Cell 1:
y, X = parser(cols, df) # output is (, )

Cell 2:
y # output is the correct dataframe

Cell 3
X # output is the correct dataframe

```

Just unsure if it is something I do wrong or if it is actually a bug with Pluto (which is by the way the reason I am playing around with Julia).

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 25, 2020, 10:23pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/9 "2020-11-25T22:23:01Z")

</div>

I think it’s just printing. Check `julia --version` when you are running Pluto vs when running as `include`. Same with `import Pkg; Pkg.status()`

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 25, 2020, 10:31pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/10 "2020-11-25T22:31:06Z")

</div>

Not sure what you mean. What is it I should exactly check?

My Julia version is 1.5.3. (type julia --version in terminal).

Against what should I compare this?

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 26, 2020, 1:09am UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/11 "2020-11-26T01:09:46Z")

</div>

Hmmm I’m not totally sure. My impression is that this is just some edge case from printing that changed between versions. But I’m not sure.

BTW, you probably don’t need to be parsing a formula string at all. You can [construct a formula programmatically](https://juliastats.org/StatsModels.jl/stable/formula/#Constructing-a-formula-programmatically-1) with Symbols.

You can also access the matrices created from that formula via `modelcols(f.rhs, df)` and `modelcols(f.lhs)` where `f` is a Formula.

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 26, 2020, 7:16am UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/12 "2020-11-26T07:16:21Z")

</div>

Thanks to all of you for your help!

@pdeffebach: yes, I know that I don’t need a string. My idea was to use a similar functionality/logic such as `patsy` in python or glm in `R` where you can pass a string formula to your df/model. Thanks for the link, anyway. I will have a look at this.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [November 26, 2020, 2:56pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/13 "2020-11-26T14:56:43Z")

</div>

It looks like `(, )` is Pluto visualisation bug, I’ve opened issue: [https://github.com/fonsp/Pluto.jl/issues/718](https://github.com/fonsp/Pluto.jl/issues/718)

Until it is fixed, you can use PlutoUI.jl functions `Print` or `with_terminal` as it is described in this issue.

---

<div class="post-metadata">

**Author:** ![fonsp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fonsp/32/222349_2.png) [@fonsp](https://discourse.julialang.org/u/fonsp)\
**Post date:** [November 26, 2020, 11:30pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/14 "2020-11-26T23:30:01Z")

</div>

Sorry!

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 27, 2020, 7:02am UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/15 "2020-11-27T07:02:07Z")

</div>

Why sorry?

I should mention that Pluto is the killer feature that had me test out Julia in the first place. I was forwarded the presentation on Pluto and was thrilled immediately. I still am.

---

<div class="post-metadata">

**Author:** ![fonsp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fonsp/32/222349_2.png) [@fonsp](https://discourse.julialang.org/u/fonsp)\
**Post date:** [November 27, 2020, 10:35am UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/16 "2020-11-27T10:35:45Z")

</div>

💞 Good to hear!

I fixed the issue in Pluto 0.12.12!

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [November 27, 2020, 1:02pm UTC](https://discourse.julialang.org/t/why-does-multiple-return-not-work/50777/17 "2020-11-27T13:02:33Z")

</div>

Cool. Thanks a lot! 😃
