C语言 如何在 Arduino 或 C 中调用函数并检索两个返回值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15415839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:42:24  来源:igfitidea点击:

How to call function and retrieve two returns in Arduino or C?

carduino

提问by Abdelrahman Elshafiey

I made a function, and I want to retrieve two returns, not a single return such as this example:

我做了一个函数,我想检索两个返回,而不是一个返回,比如这个例子:

long Conv(double num){      
    long a,b;
    a = floor(num);
    b = num * pow(10,6) - a * pow(10,6);

    return a;
    return b;
} 

When I call the function

当我调用函数时

long a = Conv(30.233456);

the question is: how do I retrieve b?

问题是:我如何检索b

回答by

You can't return two times at once.

你不能一次返回两次。

You could pass b to your function by reference.

您可以通过引用将 b 传递给您的函数。

yourfunction( long a , long* b )
{    
    *b = a + 10;
    //more code

return a;
}

a = yourfunction(a , &b );

回答by meyumer

You can't return more than one value from a function in C.Either return a struct, or pass by reference and modify in the function.

不能从 C 中的函数返回多个值。要么返回 a struct,要么通过引用传递并在函数中进行修改。

Example 1: struct

示例 1: struct

struct ab {
  long a;
  long b;
}

struct ab Conv(double num) {
  struct ab ab_instance;

  ab_instance.a = floor(num);
  ab_instance.b = num * pow(10,6) - a * pow(10,6);

  return ab_instance;
}

Example 2: pass b by reference

示例 2: pass b by reference

long Conv(double num, long& b) {
  long a;
  a = floor(num);
  b = num * pow(10,6) - a * pow(10,6);

  return a;
}

回答by Steve Westbrook

Armin kind of answered it, but here is some sample code:

Armin 有点回答它,但这里有一些示例代码:

int get_both(int* b) {
    a = 0;
    *b = 1;
    return a;
}