跨函数传递变量 [英] Passing Variables across functions

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

问题描述

基本上,我想知道如何将局部变量从函数传递到另一个函数,而变量是从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!

}

推荐答案

如果您想要1和2的值,那为什么不让函数返回它们而不是void函数呢?

例如
If you want the value of one and two, then why not have your functions return them instead of being void functions?

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


如果要从函数中获取某些信息,它应该返回一个值.局部变量之所以称为局部变量,是因为它们仅在定义它们的上下文中可见.我将尝试向您显示错误的位置:

If you want something from a function it should return a value. Local variables are called local because they are only visible in the context they were defined in. I''ll try to show you where you erred:

// 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;
}


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

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