# Is it possible to make this function work in Julia?

**URL:** <https://discourse.julialang.org/t/is-it-possible-to-make-this-function-work-in-julia/41835>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 22, 2020, 3:00am UTC](https://discourse.julialang.org/t/is-it-possible-to-make-this-function-work-in-julia/41835 "2020-06-22T03:00:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![reb](https://avatars.discourse-cdn.com/v4/letter/r/51bf81/32.png) [@reb](https://discourse.julialang.org/u/reb)\
**Post date:** [June 22, 2020, 3:00am UTC](https://discourse.julialang.org/t/is-it-possible-to-make-this-function-work-in-julia/41835/1 "2020-06-22T03:00:37Z")

</div>

Sorry this takes so much verbiage. I know that most of the functions I need to write will have several variables already defined prior to the definition of the function. The value of these variables must be accessible within the body of the function and must be able to be modified within the body of the function. The revised values must be returned by the function after which the returned values can be used to update the original values of those variables.

The concept that I thought was feasible was to pass the initial values of the variables as arguments of the function. And the revised values, created when the function is called, would be returned by the function as a tuple of whatever size is required. The values of the components of the returned tuple could then be used to update the original values of the variables.

So far I haven’t been able to get this to work. I don’t know whether this is because this simply isn’t possible in Julia or because I’m just missing a couple crucial ingredients. Here’s the example that I haven’t gotten to work:

# ftest1.jl a test program to see if a function can overcome scope problems.

arr = [“abc”,“def”,“ghi”,“jkl”,“mno”]  
x = 15  
y = 25  
println(“Prior to function definition: arr=”,arr," x=“,x,” y=“,y)  
function Example(arri,xi,yi)  
xi = xi + yi  
yi = xi \* yi  
arrt = arri  
arri[1] = arrt[5], arri[2]=arrt[4], arri[4] =arrt[2], arri[5]=arrt[1]  
println(“Within function definition: arri=”,arri,” xi=“,xi,” yi=“,yi)  
return arri,xi,yi  
end  
(arr,x,y) = Example(arr,x,y)  
println(“After completion of function call: arr=”,arr,” x=“,x,” y=",y)

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [June 22, 2020, 3:01am UTC](https://discourse.julialang.org/t/is-it-possible-to-make-this-function-work-in-julia/41835/2 "2020-06-22T03:01:27Z")

</div>

Please use ``` around code to make it format properly. It makes it much easier to read.

---

<div class="post-metadata">

**Author:** ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)\
**Post date:** [June 22, 2020, 3:15am UTC](https://discourse.julialang.org/t/is-it-possible-to-make-this-function-work-in-julia/41835/3 "2020-06-22T03:15:36Z")

</div>

All that is needed to make your code work is to replace

> [@reb](#):
>
> arri[1] = arrt[5], arri[2]=arrt[4], arri[4] =arrt[2], arri[5]=arrt[1]

with

```julia
arri[1] = arrt[5]; arri[2]=arrt[4]; arri[4] =arrt[2]; arri[5]=arrt[1]

```

(i.e. `;` instead of `,`)

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [June 22, 2020, 1:01pm UTC](https://discourse.julialang.org/t/is-it-possible-to-make-this-function-work-in-julia/41835/4 "2020-06-22T13:01:28Z")

</div>

Your function mutates the input array. It does not need to be returned.

```julia
arr = ["abc","def","ghi","jkl","mno"]
x = 15
y = 25
println("Prior to function definition: arr=",arr," x=",x," y=",y)

function Example!(arri,xi,yi)
    xi = xi + yi
    yi = xi * yi
    arrt = arri
    arri[1] = arrt[5]; arri[2]=arrt[4]; arri[4] = arrt[2]; arri[5]=arrt[1];
    println("Within function definition: arri=",arri," xi=",xi," yi=",yi)
    return xi,yi
end

x, y = Example!(arr,x,y)
println("After completion of function call: arr=",arr," x=",x," y=",y)

```

```julia
julia> include("test1.jl")
Prior to function definition: arr=["abc", "def", "ghi", "jkl", "mno"] x=15 y=25
Within function definition: arri=["mno", "jkl", "ghi", "jkl", "mno"] xi=40 yi=1000
After completion of function call: arr=["mno", "jkl", "ghi", "jkl", "mno"] x=40 y=1000

```
