Click here to Skip to main content
15,042,336 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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!

}
Posted
Updated 2-Jan-11 14:51pm
v3

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;
}
   
Comments
WurmInfinity 2-Jan-11 20:21pm
   
return it to where? if that returns it to main, its not what I want I want it in the final function. Sorry i'm new to this by the way.
T2102 2-Jan-11 20:25pm
   
Example: Instead of writing up(speed) you can write float up_value=up(speed).
Alternatively, you could make one and two global static variables. However, that's a practice you want to avoid whenever possible.
WurmInfinity 2-Jan-11 20:29pm
   
Yeah we've been told not to use them so im trying to work this out. Right so do I change the prototype from a void to a float? and then call it in final() as float one=up(one) ?
T2102 2-Jan-11 20:31pm
   
Yes, change the prototype and definition to return a float. Then you'd change final to void final(float one, float two) or final(float const & one, float const & two).
WurmInfinity 2-Jan-11 20:33pm
   
ok shall give it a go thanks
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:

C++
// 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.
   
Comments
WurmInfinity 2-Jan-11 20:38pm
   
It was an example, I wrote up quickly just to get a question answerd. My actual code is far longer and I dont want to paste it in because I want to learn not just have it alterd by someone else so I created a simular code that was far shorter. If you had read the previous answers you would know im a beginner so theres no need to be snide.
Manfred Rudolf Bihy 2-Jan-11 20:51pm
   
Well when I started to write my answer, there were not other answers. I was just very thourogh in pointing out what you did wrong. So please don't be snotty. I'm doing this on my time and we are all here to learn from on another. Others may be faster in their responses, but I take my time to give you the help you need to understand and solve a problem on your own.
WurmInfinity 2-Jan-11 20:55pm
   
Dont get me wrong im greatfull for all the help, however you havent posted anything that has actually helped me you have just pointed out my downfalls wich I gather is usefull but as I said it was rushed as an example. Maybe you could correct it instead? So I can actually see where I went wrong. Thanks
Manfred Rudolf Bihy 2-Jan-11 21:02pm
   
Thank you for that valuable information. If my post did not help you, there is not very much I can do for you especially if you cannot learn from your mistakes even when they are pointed out to you. I'm sorry that I did not provide a prefabricated solution for you. Maybe I can serve you better at another time.
Bye!
Espen Harlinn 3-Jan-11 8:45am
   
5+, Isn't it a joy to help people?
C++
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;
}
   
Comments
Manfred Rudolf Bihy 2-Jan-11 20:44pm
   
Two functions with the same signature and the down and right functions completely missing? At least you got the part with returning values and passing parameters right.
3 from me!
Stop you caught that with the declaration of the prototypes.
Make that 4!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900