# For loop not working as expected

**URL:** https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532
**Category:** General Usage
**Tags:** question
**Created:** [January 22, 2018, 9:14pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532 "2018-01-22T21:14:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [January 22, 2018, 9:14pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532/1 "2018-01-22T21:14:38Z")

</div>

My code is as below:

```julia
using FinancialToolbox

    r=0.01*0.303
    q=0.01*0.202
    s=0.20
    T=0.50
    b=r-q
    S=1500 
    K=1000:5:2000
    
    
    for i = 1:length(K)
        res1 = zeros(length(K))
        res1[i] = blsprice(S, K[i], r, T, s, q)
    end

```

It seems the res1 I got is still zero arrays, and I am not sure what could be the issue.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [January 22, 2018, 9:19pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532/2 "2018-01-22T21:19:33Z")

</div>

> [@Yifan\_Liu](#):
>
> res1 = zeros(length(K))

You are using `res1 = zeros(length(K))` at each loop iteration, what do you expect at the nth iteration other than an all-zeros array with only `res1[n]` having nonzero value?

Move this line `res1 = zeros(length(K))` before the for-loop.

```
⋮
res1 = zeros(length(K))
for i = 1:length(K)
    res1[i] = blsprice(S, K[i], r, T, s, q)
end

```

Outputs:

```
julia> res1
	201-element Array{Float64,1}:
	 500.103
	 495.124
	 490.147
	 485.171
	 480.197
	 475.225
	 470.255
	 465.288
	 460.323
	 455.362
	 450.403
	 445.447
	 440.495
	   ⋮
	   3.15828
	   3.0192
	   2.88581
	   2.7579
	   2.63527
	   2.51772
	   2.40506
	   2.29711
	   2.19369
	   2.09462
	   1.99974
	   1.90889

```

---

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [January 22, 2018, 9:52pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532/3 "2018-01-22T21:52:20Z")

</div>

HAHA, thanks so much! By the way, do you know if the blsprice function has a call/put parameter input? I could not find any documentation about the financialtoolbox package.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [January 22, 2018, 10:15pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532/4 "2018-01-22T22:15:10Z")

</div>

I didn’t use this package my self, but you can see the implementation of this function at [this line in the source of FinancialToolbox.jl](https://github.com/rcalxrc08/FinancialToolbox.jl/blob/9a6f86fe3b4f14d884183d194efbd564ad079ef3/src/financial.jl#L31).

---

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [January 22, 2018, 10:43pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532/5 "2018-01-22T22:43:03Z")

</div>

I use this code

`blsprice(10.0,10.0,0.01,2.0,0.2,0.01, FlagIsCall = false)`

but got the error message

ERROR: function blsprice does not accept keyword arguments

---

<div class="post-metadata">

### Author: ![Non-Contradiction](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/non-contradiction/32/2208_2.png) [@Non-Contradiction](https://discourse.julialang.org/u/Non-Contradiction)
#### Post date: [January 22, 2018, 10:55pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532/6 "2018-01-22T22:55:16Z")

</div>

`FlagIsCall = false` is keyword argument.  
Following the error message, you should use `blsprice(10.0,10.0,0.01,2.0,0.2,0.01, false)` instead.  
For further information on keyword argument, see: [https://docs.julialang.org/en/stable/manual/functions/#Keyword-Arguments-1](https://docs.julialang.org/en/stable/manual/functions/#Keyword-Arguments-1)

---

<div class="post-metadata">

### Author: ![NaOH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/naoh/32/5632_2.png) [@NaOH](https://discourse.julialang.org/u/NaOH)
#### Post date: [January 22, 2018, 11:21pm UTC](https://discourse.julialang.org/t/for-loop-not-working-as-expected/8532/7 "2018-01-22T23:21:35Z")

</div>

`FlagIsCall` is **not** a keyword argument, it is an optional positional argument!

```julia
julia> foo(positional, optional = true) = (positional, optional)
foo (generic function with 2 methods)

julia> foo(false)
(false, true)

julia> foo(false, false)
(false, false)

julia> foo(false, optional = false)
ERROR: function foo does not accept keyword arguments
Stacktrace:
 [1] kwfunc(::Any) at ./boot.jl:237

```

Keyword arguments are declared after a semicalon `;` in the parameter list:

```julia
julia> bar(positional, optional = true; keyword = 42) = (positional, optional, keyword)
bar (generic function with 2 methods)

julia> bar(false)
(false, true, 42)

julia> bar(false, false)
(false, false, 42)

julia> bar(false, false, keyword = 7)
(false, false, 7)

```
