# Error: function undefined

**URL:** https://discourse.julialang.org/t/error-function-undefined/23717
**Category:** New to Julia
**Tags:** question
**Created:** [April 30, 2019, 9:57pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717 "2019-04-30T21:57:05Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)
#### Post date: [April 30, 2019, 9:57pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/1 "2019-04-30T21:57:06Z")

</div>

I have worked with user defned functions before but in this case I just can’t figure out what am doing wrong. I get the error `"deCasteljauPoints not defined"`. Where is the error in my code?

```julia
function deCasteljauPoints!(points, t)
        res = []
        res.append(points)
        while length[points]>=2
            a=range(-30,stop=-1, length=100)
            b=range(1,stop=30, length=100)
            points = [(1-t) * p1 + t * p2 for (p1, p2) in zip(points(a,b))]
            res.append(points)
        return res
        end
end

points = [(-2.0, 2.0), (-3.0, 3.0), (-1.0, 2.0), (2.0, 3.0), (-1.0, 3.0)]
points = Array(points)
t=0.7
Bp=deCasteljauPoints(points,t)
Bp

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [April 30, 2019, 9:59pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/2 "2019-04-30T21:59:02Z")

</div>

You are defining your function with an added exclamation mark, but calling it without?

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [April 30, 2019, 10:21pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/3 "2019-04-30T22:21:52Z")

</div>

I’m not sure what you want your code to do, but here are a few things I noticed:

1. ` points = Array(points)` does not appear to be necessary
2. ` length[points]` should be `length(points)` because `length` is a function
3. You probably want to omit ` res = []` and use append!(points, newPoints) to add new values to points.
4. If you intend to modify points (adding ! to a function is the convention for mutation), you do not need to return anything. Otherwise, you probably want the return statement outside of your while loop.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [April 30, 2019, 11:53pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/4 "2019-04-30T23:53:31Z")

</div>

> [@Christopher\_Fisher](#):
>
> If you intend to modify points (adding ! to a function is the convention for mutation), you do not need to return anything.

It’s conventional in Julia for mutating functions to return something, usually the mutated argument. This allows the function call to be chained as an argument to another function.

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [May 1, 2019, 12:42am UTC](https://discourse.julialang.org/t/error-function-undefined/23717/5 "2019-05-01T00:42:42Z")

</div>

Thanks for clarifying. I didn’t realize that functions like `push!` return the mutated object. I can see how that would be useful for chaining.

---

<div class="post-metadata">

### Author: ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)
#### Post date: [May 1, 2019, 12:22pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/6 "2019-05-01T12:22:48Z")

</div>

I made the appropriate corrections and did some more reading. What I want my code to do is to simply give me a list of points in between a range `a` & `b` within a time `t`.  
How do I correctly define newPoints assuming I need to define it.  
The error I have now is `cannot `convert` an object of type Array{Tuple{Float64,Float64},1} to an object of type Tuple{Float64,Float64}` which am thinking stems from my `points` being listed as an array of points.

Here is the code:

```julia
function deCasteljauPoints(points, t)
        append!(points, newPoints)
        while length(points)>=2
            a=range(-30,stop=-1, length=100)
            b=range(1,stop=30, length=100)
            points = [(1-t) * p1 + t * p2 for (p1, p2) in zip(points(a,b))]
            append!(points, newPoints)
            end
            return points
end
points = [(-2.0, 2.0), (-3.0, 3.0), (-1.0, 2.0), (2.0, 3.0), (-1.0, 3.0)]
#newPoints=?
t=0.7
B=deCasteljauPoints(points,t)
B
```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 1, 2019, 12:35pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/7 "2019-05-01T12:35:03Z")

</div>

Please make sure to reduce your code to a minimum working example (MWE) that reproduces the error that you’re seeing and that others can run, have a look at: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

If I copy the code in your post above, I get the error `newPoints not defined`, which occurs on the very first line of your `deCasteljauPoints` function (where you do `append!(points, newPoints)`, without defining `newPoints` first)

---

<div class="post-metadata">

### Author: ![Angela\_Mulenga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/angela_mulenga/32/7507_2.png) [@Angela\_Mulenga](https://discourse.julialang.org/u/Angela_Mulenga)
#### Post date: [May 1, 2019, 2:51pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/8 "2019-05-01T14:51:08Z")

</div>

How do I define newPoints? I dont know how to do that and I will first put the defined newPoints before calling it in my function.I read the link too,thankyou.

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [May 1, 2019, 3:44pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/9 "2019-05-01T15:44:54Z")

</div>

Well that seems to me to be more of a question on your application and intent rather than on how to code it? Nowhere in your code are newPoints declared, and it’s impossible to guess what the correct definition would be without understanding what you are actually trying to do.

Your code snippet quite frankly doesn’t make a lot of sense and there are a few potential problems in there, my best guess is that you’re trying to do something like

```julia
function getpoints(t) 
    a=range(-30,stop=-1, length=100) 
    b=range(1,stop=30, length=100)
    return [(1-t) * p1 + t * p2 for (p1, p2) in zip(a, b)]
end

```

But this is a pretty wild guess based on those parts of your original function which actually do something…

---

<div class="post-metadata">

### Author: ![ElOceanografo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eloceanografo/32/624_2.png) [@ElOceanografo](https://discourse.julialang.org/u/ElOceanografo)
#### Post date: [May 1, 2019, 4:08pm UTC](https://discourse.julialang.org/t/error-function-undefined/23717/10 "2019-05-01T16:08:32Z")

</div>

Yes, it seems like you have a bit of confusion about the algorithm that you should figure out first. Are you trying to implement [this](https://en.wikipedia.org/wiki/De_Casteljau's_algorithm)? I’m not familiar with the algorithm, but I found this StackOverflow question and translated that Python function into Julia here:

```julia
function deCasteljau(points, t)
    tmp_points = copy(points)
    while length(tmp_points) > 1
        for k in 1:length(tmp_points)-1
            pnt = tmp_points[k] * (1 - t) + tmp_points[k+1] * t
            tmp_points[k] = pnt
        end
        tmp_points = tmp_points[1:end-1]
    end
    return tmp_points[1]
end
        
points = [[-2.0, 2.0], [-3.0, 3.0], [-1.0, 2.0], [2.0, 3.0], [-1.0, 3.0]]
t=0.7
deCasteljau(points,t)

```

I don’t think this is doing quite what you want your function to do (what are the ranges inside your loop for?), but maybe it will be helpful as a starting point…
