# How to Fix Error : ("syntax: cannot add method to function argument p")

**URL:** <https://discourse.julialang.org/t/how-to-fix-error-syntax-cannot-add-method-to-function-argument-p/23742>\
**Category:** New to Julia\
**Created:** [May 1, 2019, 5:52pm UTC](https://discourse.julialang.org/t/how-to-fix-error-syntax-cannot-add-method-to-function-argument-p/23742 "2019-05-01T17:52:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Engineer43](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Engineer43](https://discourse.julialang.org/u/Engineer43)\
**Post date:** [May 1, 2019, 5:52pm UTC](https://discourse.julialang.org/t/how-to-fix-error-syntax-cannot-add-method-to-function-argument-p/23742/1 "2019-05-01T17:52:32Z")

</div>

Hi guys, I am trying to say that p in the function title is a function of x (it’s a penalty function that depends on if the constraints [c] are violated) but I keep getting this error. How can I re-classify p or change it so that p(x) is used in the equation and not just p? Furthermore, c is a 1x2 vector with two equations. Would the way I am using c also mean that it is evaluating c at point x, and then placing that value into p(x)? Thanks!

function penalty\_method(f, p, x, k\_max, c; ρ=2, γ=8)  
for k in 1 : k\_max  
cs = c(x)  
println("k: ", k)  
p(z)=sum(c(z) .\> 0)  
x = hooke\_jeeves(x → f(x) + ρ\*p(x), x, 1, .01, 2)  
ρ \*= γ  
println("c(x): ", c(x))  
if c(x)[1] \<= 0 && c(x)[2] \<= 0  
return x  
end  
end  
# println(“return!!”)  
return x  
end

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [May 1, 2019, 6:05pm UTC](https://discourse.julialang.org/t/how-to-fix-error-syntax-cannot-add-method-to-function-argument-p/23742/2 "2019-05-01T18:05:21Z")

</div>

The problem is that you’re using `p` as a function argument and then also trying to define a generic function `p(x)` inside the function. Since it doesn’t seem like you’re using the function argument, maybe you need to drop it?

---

<div class="post-metadata">

**Author:** ![Engineer43](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Engineer43](https://discourse.julialang.org/u/Engineer43)\
**Post date:** [May 1, 2019, 6:17pm UTC](https://discourse.julialang.org/t/how-to-fix-error-syntax-cannot-add-method-to-function-argument-p/23742/3 "2019-05-01T18:17:48Z")

</div>

I’ll try that really quickly. Thanks.

---

<div class="post-metadata">

**Author:** ![Engineer43](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Engineer43](https://discourse.julialang.org/u/Engineer43)\
**Post date:** [May 1, 2019, 6:54pm UTC](https://discourse.julialang.org/t/how-to-fix-error-syntax-cannot-add-method-to-function-argument-p/23742/4 "2019-05-01T18:54:53Z")

</div>

TLDR: Ideally, I want the function\_optimize to run as few iterations as possible to get the function f to the constrained region, and then minimized within the constrained region.

Here’s a snapshot of what two iterations looks like:  
Calling optimize  
k: 1  
c(x): [1.21346, -2.20998]  
k: 2  
c(x): [1.21346, -2.20998]  
k: 3  
c(x): [1.21346, -2.20998]  
k: 4  
c(x): [1.21346, -2.20998]  
k: 5  
c(x): [1.21346, -2.20998]  
k: 6  
c(x): [1.21346, -2.20998]  
k: 7  
c(x): [1.21346, -2.20998]  
k: 8  
c(x): [1.21346, -2.20998]  
k: 9  
c(x): [1.21346, -2.20998]  
k: 10  
c(x): [1.21346, -2.20998]  
Calling optimize  
k: 1  
c(x): [0.97639, -2.0323]  
k: 2  
c(x): [0.97639, -2.0323]  
k: 3  
c(x): [0.97639, -2.0323]  
k: 4  
c(x): [0.97639, -2.0323]  
k: 5  
c(x): [0.97639, -2.0323]  
k: 6  
c(x): [0.97639, -2.0323]  
k: 7  
c(x): [0.97639, -2.0323]  
k: 8  
c(x): [0.97639, -2.0323]  
k: 9  
c(x): [0.97639, -2.0323]  
k: 10  
c(x): [0.97639, -2.0323]  
Calling optimize

My goal is essentially this: Use function\_optimize to call the penalty\_method function and use the f, g, and c given by the function\_optimize which is another file. Within the penalty\_method function, I want to use the Hooke-Jeeves algorithm to minimize the penalty function value, which is f(x)+ rho\*p(x) where p(x) is either, 0, 1, or 2 since there are two constraints that may be violated. I want that to be minimized and stopped, meaning that both constraints c(x)[1] and c(x)[2] are not violated, so c(x) is equal to zero, and therefore p(x) is. That will give me the x-value of a feasible point that then needs to be transferred to the interior points method function. This function will then use Hooke-Jeeves once again to minimize its value toward to global minimum, while remaining in the appropriate constrained region. However, it appears that Hooke-Jeeves is not pushing the function towards an x-value of feasibility, and the code just keeps resetting and running the optimize function instead. Ideally, I want the function optimize to run as few iterations as possible to get the function f, to the constrained region and minimized.

---

<div class="post-metadata">

**Author:** ![Engineer43](https://avatars.discourse-cdn.com/v4/letter/e/2acd7d/32.png) [@Engineer43](https://discourse.julialang.org/u/Engineer43)\
**Post date:** [May 1, 2019, 6:56pm UTC](https://discourse.julialang.org/t/how-to-fix-error-syntax-cannot-add-method-to-function-argument-p/23742/5 "2019-05-01T18:56:10Z")

</div>

![14%20AM](https://global.discourse-cdn.com/julialang/original/3X/e/0/e0094b7807e55a8be437a2ab88a66918cbff66c1.png) ![26%20AM](https://global.discourse-cdn.com/julialang/original/3X/7/0/704b6b4cbbe878b97ccebdc68cce72c847deefc3.png)

This is my full code
