函数返回 2 个值给全局变量 [英] function returning 2 values to global variable

查看:64
本文介绍了函数返回 2 个值给全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于这篇文章,我想问一下为什么下面的脚本适用于 [a,b] 但不适用于 [c,d].
找不到任何解释为什么这不起作用的文档.

In light of this post I'd like to ask why the script hereunder works for [a,b] but doesn't work for [c,d].
Cannot find any documentation that explains why this doesn't work.

此示例仅适用于 2 个返回值,但实际上我将创建一个函数,其中包含 6 个或更多要一次性返回的变量.
我试图避免输入 6 行不同的行,因为我将在每个交易日输入这些数据(该函数将与日期相关,而且我已经有了代码).
所以我想每天只需要输入 1 行,以保持源代码清晰和可维护.

This example is only for 2 return values, but in reality I'm going to create a function with 6 or more variables to be returned in one go.
I'm trying to avoid having to enter 6 different lines, because I'll be entering this data every trading day (the function will be date-depentent and I already have code for that).
So I'd like to only have to enter 1 line per day to keep the source code clear and maintainable.

//@version=4
study("Functions test")

var int c = na
var int d = na

f(x) => [x,x+5]

[a,b] = f(20)
[c,d] := f(30)

plot(a)
plot(b)
plot(c)
plot(d)

推荐答案

我的理解是使用 := 赋值是不允许用于类似元组的函数返回.如果您想避免多次输入函数输入,在本例中为 20 和 30,同时保持变量定义不变,您仍然可以执行以下操作:

My understanding is that assigning with := is not allowed for tuple-like function resturns. If you want to avoid entering multiple times the function input, in this case, 20 and 30, while keeping the variable definition as it is, you can still do something like:

//@version=4
study("Functions test")

var int c = na
var int d = na

f(x) => [x,x+5]

[a,b] = f(20)
[c1,d1] = f(30)

c := c1
d := d1

plot(a)
plot(b)
plot(c)
plot(d)

它确实需要几行额外的行,而且看起来很丑,但至少您将必须根据需要键入函数输​​入的次数限制为一次.

It does require several extra lines, and looks ugly, but at least you limit to one the number of times you have to type the input to the function as desired.

这篇关于函数返回 2 个值给全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆