Alex100
Pine Script Rookie
Pine Script Rookie
Posts: 16
Joined: March 14th, 2022

Call a function from inside a loop

I'd like to call a function from inside a 'for' loop. I wrote the code below, but I get an 'undeclared identifier' error.

I tried lots of things to make it run, but couldn't find any solution.

Can someone please help me?

Alex

Code: Select all

//@version=5
indicator("My function")

my_function(i) =>
    // code here...
    [i]

for i = 1 to 10 by 1
    [result] = my_function(i)

plot (result)

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Call a function from inside a loop

Hey,

If you can complete your code a bit more, it would be easier to help you out in this.

Error "Undeclared Identifier" means you need to initialize the variable before you can use it.

Alex100
Pine Script Rookie
Pine Script Rookie
Posts: 16
Joined: March 14th, 2022

Re: Call a function from inside a loop

I already tried to declare the 'result' variable, but I still received an error.

If I declare it at the top of the script I get this error...

Shadowing variable 'result' which exists in parent scope.

and if I declare it right beneath the 'for' instruction, I get this one...

'result' is already defined.

I tried mostly everything I could think of, but nothing seemed to work.

Alex

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Call a function from inside a loop

When you declare the variable outside , and than want to use it again , you need to make sure you use

Code: Select all

:=
not just plain

Code: Select all

=

Code: Select all

float result = na

result = 1.0   // WRONG
result := 1.0  // CORRECT

Alex100
Pine Script Rookie
Pine Script Rookie
Posts: 16
Joined: March 14th, 2022

Re: Call a function from inside a loop

Ok, I no longer receive the two errors in my previous message, but a new one is showing up now, and I can't get rid of it:

line 11: Syntax error

I don't see any syntax error on line 11 (which is 'result := 1.0'), so what am I doing wrong? The code is this:

Code: Select all

//@version=5
indicator("My function")

float result = na

my_function(i) =>
    // code here...
    [i]

for i = 1 to 10 by 1
    result := 1.0
    [result] = my_function(i)

plot (result)

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Call a function from inside a loop

Do you have more code, so i can better understand exactly what you are trying to do ? It is not clear , whether you are creating arrays, or how your my_function works.... rather than // code here, if you can provide more code and explain what you intend it to do. Even if it has errors, i will atleast be able to understand the logic.

Alex100
Pine Script Rookie
Pine Script Rookie
Posts: 16
Joined: March 14th, 2022

Re: Call a function from inside a loop

Since I first posted my initial question, I did find a better solution that I used to replace this function, so I no longer have any code for it.

But to give you a basic example, the function can calculate the percentage of a number from another number. And in case you need something like this in multiple locations within a script (including a 'for' loop), you can simply call the function instead of writing the same code multiple times.

I'm not trying to create any arrays, I just need the basic functionality of any function: access the same code from multiple locations within a script.

processingclouds
Pine Script Master
Pine Script Master
Posts: 115
Joined: January 30th, 2022

Re: Call a function from inside a loop

Ok, so you are looking for a way to have a global variable that can be changed by a function. Here is a small indicator to show this :

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © processingclouds


//@version=5
indicator("My function", overlay=true)

result = array.new_float(1, 0.0)


my_function(i) =>
    oldValue = array.get(result, 0)
    array.set(result,0, oldValue + i)

for i = 1 to 10 by 1
    my_function(i*1.1)

if barstate.islast
    label.new(bar_index, high, str.tostring(array.get(result,0)), textcolor=color.white)


Alex100
Pine Script Rookie
Pine Script Rookie
Posts: 16
Joined: March 14th, 2022

Re: Call a function from inside a loop

Works great, thank you very much!

Return to “Pine Script Q&A”