# Argument passing with dataframes

**URL:** <https://discourse.julialang.org/t/argument-passing-with-dataframes/64355>\
**Category:** New to Julia\
**Tags:** dataframes\
**Created:** [July 9, 2021, 1:59pm UTC](https://discourse.julialang.org/t/argument-passing-with-dataframes/64355 "2021-07-09T13:59:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [July 9, 2021, 1:59pm UTC](https://discourse.julialang.org/t/argument-passing-with-dataframes/64355/1 "2021-07-09T13:59:05Z")

</div>

Hello!

I am wondering the following with dataframes:

```julia
function addcol1(x)
	x.mycol = x.ID .+ 1
end

function join1(x)
	jobs = DataFrame(ID = [20, 40,45], Job = ["Lawyer", "Doctor","teacher"])
	x = innerjoin(x, jobs, on = :ID)
	return x
end

```

Run this with

```julia
people = DataFrame(ID = [20, 40,45], name = ["Jane", "Kalle","Petri"], h = [10,16,25])

addcol1(people)
join1(people)

```

The first command changes variable people in the calling scope, the second does not. Why?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 9, 2021, 2:39pm UTC](https://discourse.julialang.org/t/argument-passing-with-dataframes/64355/2 "2021-07-09T14:39:42Z")

</div>

In the `join1` function, initially the variable `x` represents the object passed as argument. Later you do this:

```julia
x = innerjoin(x, jobs, on = :ID)

```

This means that `x` now represents a different object: the return value of `innerjoin`. It does **not** mean that the previous object will be changed.

This is a common source of confusion for people used to some other programming languages. See [this FAQ entry](https://docs.julialang.org/en/v1/manual/faq/#I-passed-an-argument-x-to-a-function,-modified-it-inside-that-function,-but-on-the-outside,-the-variable-x-is-still-unchanged.-Why?-1).

---

<div class="post-metadata">

**Author:** ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)\
**Post date:** [July 9, 2021, 3:46pm UTC](https://discourse.julialang.org/t/argument-passing-with-dataframes/64355/3 "2021-07-09T15:46:29Z")

</div>

> [@Mastomaki](#):
>
> `x.mycol = x.ID .+ 1`

Also, this is a mutating operation. There are no mutating operations in the 2nd version.

---

<div class="post-metadata">

**Author:** ![Mastomaki](https://avatars.discourse-cdn.com/v4/letter/m/779978/32.png) [@Mastomaki](https://discourse.julialang.org/u/Mastomaki)\
**Post date:** [July 9, 2021, 5:04pm UTC](https://discourse.julialang.org/t/argument-passing-with-dataframes/64355/4 "2021-07-09T17:04:10Z")

</div>

So whenever there is assignment

```julia
x = something

```

this means that the connection to the original object is lost?

My next question is: should I return the resulting dataframe as return value; or is there a way to copy the result of innerjoin to x so that x still represents the original object?

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [July 9, 2021, 6:10pm UTC](https://discourse.julialang.org/t/argument-passing-with-dataframes/64355/5 "2021-07-09T18:10:29Z")

</div>

> [@Mastomaki](#):
>
> this means that the connection to the original object is lost?

Yes, exactly!

> [@Mastomaki](#):
>
> should I return the resulting dataframe as return value; or is there a way to copy the result of innerjoin to x so that x still represents the original object?

You should return it as a return value. If you were just adding a column for example, you could modify the existing object. But with a “join” operation you create a new object, so you should return it.

You could go around these rules by defining a macro instead of a function, but that’s generally a bad idea (macros should only be used for good reason as they make the code less readable/predictable and they are more difficult to write without bugs).
