# Returning to wrong result with multiple return values

**URL:** <https://discourse.julialang.org/t/returning-to-wrong-result-with-multiple-return-values/100104>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 9, 2023, 1:15pm UTC](https://discourse.julialang.org/t/returning-to-wrong-result-with-multiple-return-values/100104 "2023-06-09T13:15:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [June 9, 2023, 1:15pm UTC](https://discourse.julialang.org/t/returning-to-wrong-result-with-multiple-return-values/100104/1 "2023-06-09T13:15:40Z")

</div>

I used Newton interpolation for function q and Horner’s algorithm for the evaluation, here is the code which works correctly;

```julia
function Q(q,t)
  d = q;   
# create divided difference
    for j = 1:length(q)-1
    for i = length(q):-1:j+1    
        d[i] = (d[i]-d[i-1])/(η[i]-η[i-j]); 
    end
    end
# the evaluation    
    p = d[end] ; dp = 0;
  for i = length(q)-1:-1:1 
          dp = p + (t-η[i]) * dp
          p = d[i] + p*(t-η[i])
          
    end
    return p, dp; #p returns to Q(t) and dp returns to Q'(t)
end

```

As an example if we take `η = [1.0, 1.3, 1.6, 1.9, 2.2]; q = [0.7651977, 0.6200860 , 0.4554022, 0.2818186, 0.1103623]`, then `Q(q , 1.5)` it gives me the correct result `(0.5118199942386832, -0.5578831893004117)`.

My problem is, I would like to create two functions return to `Q(q , t)[1]` and `Q(q , t)[2]` like `q̃ = (q,t) -> Q(q,t)[1] ; q̃′ = (q,t) -> Q(q,t)[2]` but the result is `(0.5118199942386832, 1.6121262983073357)` which is wrong for `Q(q , t)[2]`.

---

<div class="post-metadata">

**Author:** ![mbaz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbaz/32/17295_2.png) [@mbaz](https://discourse.julialang.org/u/mbaz)\
**Post date:** [June 9, 2023, 1:22pm UTC](https://discourse.julialang.org/t/returning-to-wrong-result-with-multiple-return-values/100104/2 "2023-06-09T13:22:19Z")

</div>

The problem is that `Q` modifies its argument `q`. `d = q` does not create a new vector; it just defines a new name for the same vector. The solution is to use `d = copy(q)`.

---

<div class="post-metadata">

**Author:** ![khaledharizb](https://avatars.discourse-cdn.com/v4/letter/k/57b2e6/32.png) [@khaledharizb](https://discourse.julialang.org/u/khaledharizb)\
**Post date:** [June 9, 2023, 1:28pm UTC](https://discourse.julialang.org/t/returning-to-wrong-result-with-multiple-return-values/100104/3 "2023-06-09T13:28:28Z")

</div>

Yeah, it works very well, thank you @mbaz
