本文介绍了跨函数传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想知道如何将局部变量从函数传递到另一个函数,而变量是从main传递到那些函数的.下面是一个示例:

Hi, basically I want to know how to pass local variables from functions to another function, whilst variables are being passed from main to those functions. Below is an example:

int main(int argc, char *argv[])
{
     void up(float& speed);
     void down(float& speed);
     void final();

     float speed;

     cin << speed;

     up(speed);
     down(speed);
     right(speed);
     final();
}

void up(float& speed)
{
     float one;

     if(speed = 1)
     {
        one = 10;
     }
     else if(speed = 2)
     {
        one = 20;
     }
}

void down(float& speed)
{
     float two;
     if(speed = 1)
     {
        two = 10;
     }
     else if(speed = 2)
     {
        two = 20;
     }
}

void final()
{
     cout << one + two;
     //---- This is the bit I dont get. I want the variables one and two from the previous functions but I have already passed ''speed'' through and am not sure how to do this. Please help!

}

推荐答案

float up(float & speed) {
...
return two;
}


// 1. function declaration don't go inside main
void up(float&amp; speed); // This one's defined twice! Why?
void down(float&amp; speed); // It's been declared allright but defined
void final();


int main(int argc, char *argv[])
{
    float speed;
    cin << speed;
    up(speed); // function speed is void so it doesn't return anything
    //down(speed); You did supply a declaration but not a definition
    //right(speed); You didn't supply a declaration and neither a definition
    final(); // This function doesn't have any parameters so it can't know the values that were not returned by the functions
}
void up(float& speed) // should return float
{
     float one;
     if(speed = 1)
     {
        one = 10;
     }
     else if(speed = 2)
     {
        one = 20;
     }
     // return one;
}

//Duplicate function definition with the same name and signature
void up(float& speed)
{
     float two;
     if(speed = 1)
     {
        two = 10;
     }
     else if(speed = 2)
     {
        two = 20;
     }
}

void final() // function does not have parameters and one and two are not global variables, so one and two in the functions body are not defined
{
     cout << one + two;
     //---- This is the bit I dont get. I want the variables one and two from the previous functions but I have already passed 'speed' through and am not sure how to do this. Please help!
}



您写的内容确实表明您完全不了解自己在做什么.您是否尝试过编译该事件.如果您有编译器,则应该为您提供改进方面的足够提示.



What you wrote really shows a total lack of understanding of what you are doing. Did you event try to compile that. If you had the compiler should have given you enough hints on what to improve.


void up(float& speed, float& one, float& two);
void down(float& speed, float& one, float& two);
void final(float& one, float& two);
int main(int argc, char *argv[])
{
      float speed;
      float one;
      float two;

      cin << speed;

      up(speed,one,two);
      down(speed,one,two);
      right(speed,one,two);
      final(one,two);
}

void up(float& speed, float& one, float& two)
{
      if(speed = 1)
      {
            one = 10;
      }
      else if(speed = 2)
      {
            one = 20;
      }
}

void up(float& speed, float& one, float& two)
{
      if(speed = 1)
      {
            two = 10;
      }
      else if(speed = 2)
      {
            two = 20;
      }
}

void final(float& one, float& two)
{
      cout << one + two;
}


这篇关于跨函数传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 15:21
查看更多