How to get an int value from calling a void function in C

如何获得一个int值从C调用void函数

Tags: c++ c
标签: c++ c

问题 (Question)

Hi there i have this line of code, i want to know how to make this function return an int value..... for some reason when i want to print the value it shows an error.

void lat_dir()//get dimensions
{
  char i=0,val;

  if( ID())
  {
    comma(3);
    while(1)
    {
      if(Serial.available())
      {
        val = Serial.read();
        Serial.write(val);
        Serial.println();
        i++;
      }
      if(i==1)
      {
        i=0;
        return;
      }  
    }
  }
}

你好我有这一行代码,我想知道如何使这个函数返回一个int值.....出于某种原因,当我想打印它显示了一个错误的价值。

void lat_dir()//get dimensions
{
  char i=0,val;

  if( ID())
  {
    comma(3);
    while(1)
    {
      if(Serial.available())
      {
        val = Serial.read();
        Serial.write(val);
        Serial.println();
        i++;
      }
      if(i==1)
      {
        i=0;
        return;
      }  
    }
  }
}

最佳答案 (Best Answer)

i want to know how to make this function return an int value

I'm not so sure what you're asking about actually, but to get a result for a reference parameter (with a plain c pointer) use something like this (always assuming your val is the 'return value' meant):

void lat_dir(char* val_ref) //get dimensions
{
  char i=0;
  *val_ref = -1; // Indicate erronous or no result

  if( ID()) {
    comma(3);
    while(1) {
      if(Serial.available()) {
        *val_ref = Serial.read();
        Serial.write(*val_ref);
        Serial.println();
        i++;
      }
      if(i==1) {
        i=0;
        return;
      }  
    }
  }
}

Call and print:

char val;
lat_dir(&val);
std::cout << val << std::endl;

The other option simply omitting the void return is:

char lat_dir()//get dimensions
{
  char i=0;
  char val = -1;

  if( ID())
  {
    comma(3);
    while(1)
    {
      if(Serial.available())
      {
        val = Serial.read();
        Serial.write(val);
        Serial.println();
        i++;
      }
      if(i==1)
      {
        i=0;
        return val;
      }  
    }
  }
  return val;
}

Call and print:

std::cout << lat_dir() << std::endl;

C++ alternative reference parameter would be

void lat_dir(char& val_ref) //get dimensions
{
  char i=0;
  val_ref = -1; // Indicate erronous or no result

  if( ID()) {
    comma(3);
    while(1) {
      if(Serial.available()) {
        val_ref = Serial.read();
        Serial.write(val_ref);
        Serial.println();
        i++;
      }
      if(i==1) {
        i=0;
        return;
      }  
    }
  }
}

Call and print:

char val;
lat_dir(val);
std::cout << val << std::endl;

我想知道如何使这个函数返回一个int值

我不太确定你所询问的,但得到的结果为参考参数(纯c指针)使用这样的(总是假设你的val“返回值”的意思):

void lat_dir(char* val_ref) //get dimensions
{
  char i=0;
  *val_ref = -1; // Indicate erronous or no result

  if( ID()) {
    comma(3);
    while(1) {
      if(Serial.available()) {
        *val_ref = Serial.read();
        Serial.write(*val_ref);
        Serial.println();
        i++;
      }
      if(i==1) {
        i=0;
        return;
      }  
    }
  }
}

电话和印刷:

char val;
lat_dir(&val);
std::cout << val << std::endl;

另一个选择简单地省略void返回:

char lat_dir()//get dimensions
{
  char i=0;
  char val = -1;

  if( ID())
  {
    comma(3);
    while(1)
    {
      if(Serial.available())
      {
        val = Serial.read();
        Serial.write(val);
        Serial.println();
        i++;
      }
      if(i==1)
      {
        i=0;
        return val;
      }  
    }
  }
  return val;
}

电话和印刷:

std::cout << lat_dir() << std::endl;

c++替代引用参��

void lat_dir(char& val_ref) //get dimensions
{
  char i=0;
  val_ref = -1; // Indicate erronous or no result

  if( ID()) {
    comma(3);
    while(1) {
      if(Serial.available()) {
        val_ref = Serial.read();
        Serial.write(val_ref);
        Serial.println();
        i++;
      }
      if(i==1) {
        i=0;
        return;
      }  
    }
  }
}

电话和印刷:

char val;
lat_dir(val);
std::cout << val << std::endl;

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/23451878