如何处理Arduino退货

如何处理Arduino退货,arduino,Arduino,我觉得问这个问题非常愚蠢,但是我如何处理返回值呢 例如,我有以下代码: int x = 0; void setup(){ Serial.begin(9600); } void loop(){ int y = calc(x); Serial.println(y); delay(500); } int calc(int nmbr){ int i = nmbr + 1; return i; } 我如何使x上升?基本上,我想看到它变成0,1,2,3,4,5等等 我知道使用

我觉得问这个问题非常愚蠢,但是我如何处理返回值呢

例如,我有以下代码:

int x = 0;

void setup(){
  Serial.begin(9600);
}

void loop(){
  int y = calc(x);
  Serial.println(y);

  delay(500);
}

int calc(int nmbr){
 int i = nmbr + 1;
 return i; 
}
我如何使x上升?基本上,我想看到它变成0,1,2,3,4,5等等 我知道使用for()很容易做到这一点,但我想知道如何使用返回值,而不是如何创建计数器


解决方案可能非常简单,当我看到它时,我会用facepalm,但我在过去的30分钟里一直在看我的屏幕,我完全被困在这个问题上。

你没有改变
x
,你在改变另一个变量
nmbr
,因为你在按值传递
x
,这是
x
的一个副本,您可以通过引用传递它,或者由于
x
是全局的,您可以这样做:

int calc() {
 return x++;
}
但实际上,您应该只使用for循环:)

intx;
对于(x=0;x尝试:


Mux的答案很好。我将添加更多种类。首先,只需将函数返回值赋回到
x

loop() {
    x = calc( x );
    Serial.println( x );
}
其次,使用callbyreference,在这里传递一个指向
x
的指针,而不是
x
的值

void loop() {
    int y = calc( &x );
    Serial.println( y );
}

int calc( int *nmbr ) {
    *nmbr++;
}
通过阅读来掌握这门语言的诀窍和它的可能性对你真的有好处。祝你好运:-)


干杯,

您可以声明静态int,而不是声明为int

#include <stdio.h>


void func() {
    static int x = 0; // x is initialized only once across three calls of func() and x will get incremented three 
                          //times after all the three calls. i.e x will be 2 finally
    printf("%d\n", x); // outputs the value of x
    x = x + 1;
}

int main() { //int argc, char *argv[] inside the main is optional in the particular program
    func(); // prints 0
    func(); // prints 1
    func(); // prints 2
    return 0;
}
#包括
void func(){
静态int x=0;//x仅在三次调用func()时初始化一次,x将递增三次
//在所有三次呼叫之后的时间。即x最终为2
printf(“%d\n”,x);//输出x的值
x=x+1;
}
int main(){//int argc,main中的char*argv[]在特定程序中是可选的
func();//打印0
func();//打印1
func();//打印2
返回0;
}

您所说的如何处理返回值是什么意思?那么,如何更改x使其增加?基本上,x应该变成calc()的返回值。我知道,我制作了这段代码作为示例。我知道解决办法会很简单,我现在真的很高兴。。。谢谢你,好的先生或女士!
void loop() {
    int y = calc( &x );
    Serial.println( y );
}

int calc( int *nmbr ) {
    *nmbr++;
}
#include <stdio.h>


void func() {
    static int x = 0; // x is initialized only once across three calls of func() and x will get incremented three 
                          //times after all the three calls. i.e x will be 2 finally
    printf("%d\n", x); // outputs the value of x
    x = x + 1;
}

int main() { //int argc, char *argv[] inside the main is optional in the particular program
    func(); // prints 0
    func(); // prints 1
    func(); // prints 2
    return 0;
}