Thread: Calling subroutines

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    12

    Calling subroutines

    I am writing with an arduino but I have a basic question dealing with subroutines.

    I have 3 subroutines, and two of them calculate a value. I want to send both of the subroutine values to one subroutine to do another calculation and was wondering how to do that. Here is an example of what I am trying to do.

    I am wanting to send 'targetPos' and 'currentPos' to the calculateError to do a calculation with both values. Not sure how to call them. Or would I just need to use both of those values and do the calculation inside main(or void loop in the arduinos case)?

    Code:
    void pSteer (const unsigned int value)
    {
      
      int steering = constrain(value, 400, 1023);
      
       int targetPos = map(steering, 400, 1023, -100, 100);
    
       calculateError(targetPos);
      
    } 
    
    void pWheel(const unsigned int value)
    {
    
      int wheel = constrain(value, 150, 480); 
      
      int currentPos = map(wheelInput, 150, 480, -100,100);
    
      calculateError(currentPos);
    }
    
    void calculateError(.........)
    {
       int error = targetPos - currentPos;
    }

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well presumably the function that you are sending the values to takes two parameters of the types of your "calculations". Then you can just call that function with your two values as arguments.

    i.e. calculateError(currentPos,targetPos);
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Ok, I am trying to and am getting an error.

    "variable or field 'calculateError' declared void"
    Trying implementing it like:
    void calculateError(targetPos,currentPos) and
    calculateError(targetPos, currentPos)

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    well that's because you haven't specified the type of the parameters in your function prototype.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    I did not just did not mention it, gave me an error like:
    "to few arguments to 'void calculateError'"

    Yes, I am sure I am missing something but just not sure what.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Too few arguments means you have not supplied the correct number of arguments to your function.

    if you declare:

    Code:
    void MyFunction(int i, int j)
    {
    
      //...
    
    }
    And then call:

    Code:
    MyFunction(i);
    Then you have supplied too few arguments
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    This compiles but not sure if it is correct for what I want. I am wanting to take the values I have gotten 'targetPos' and 'currentPos' in the previous subroutines and use them in calculateError. Will I have to make a call from main with this? Not sure if this will use those specific values or will want to be call the subroutine from main with two arguments.

    Code:
    void pSteer (const unsigned int value)
    {
       
      int steering = constrain(value, 400, 1023);
       
       int targetPos = map(steering, 400, 1023, -100, 100);
     
       //calculateError(targetPos);
    
       
    } 
     
    void pWheel(const unsigned int value)
    {
     
      int wheel = constrain(value, 150, 480); 
       
      int currentPos = map(wheelInput, 150, 480, -100,100);
     
      //calculateError(currentPos);
    
    }
     
    void calculateError(int currentPos, int targetPos)
    
    {
       int error = targetPos - currentPos;
    }

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    At the moment targetPos and currentPos are local to the functions you have delcared them in, they will not exist after the curly braces in each of those functions.

    You need to show a bit more code perhaps to clarify what your aim is, at present whatever you are trying won't work because target and current exist seperately from one another.

    You understand? At it is, As soon as pSteer() completes then targetPos has gone. And likewise in pWheel() and currentPos
    Last edited by rogster001; 04-10-2012 at 05:29 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You should have those two functions "return" what you want, and use those values as arguments for your other function.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  10. #10
    Registered User
    Join Date
    Apr 2012
    Posts
    12
    Quote Originally Posted by rogster001 View Post
    At the moment targetPos and currentPos are local to the functions you have delcared them in, they will not exist after the curly braces in each of those functions.

    You need to show a bit more code perhaps to clarify what your aim is, at present whatever you are trying won't work because target and current exist seperately from one another.

    You understand? At it is, As soon as pSteer() completes then targetPos has gone. And likewise in pWheel() and currentPos
    Yea, that is what I understand. My problem is trying to use targetPos and currentPos before they are gone. Maybe claudiu explained it with the return.

    Quote Originally Posted by claudiu View Post
    You should have those two functions "return" what you want, and use those values as arguments for your other function.
    Ok, so changed the code a little to return the value and call the calculateError function again.

    Code:
    int pSteer (const unsigned int value)
    
    {
        
      int steering = constrain(value, 400, 1023);
        
       int targetPos = map(steering, 400, 1023, -100, 100);
      
       //calculateError(targetPos);
       return targetPos;
    
    
     
        
    } 
      
    void pWheel(const unsigned int value)
    {
      
      int wheel = constrain(value, 150, 480); 
        
      int currentPos = map(wheelInput, 150, 480, -100,100);
      
      //calculateError(currentPos);
      return currentPos;
    
    
     
    }
      
    void calculateError(int currentPos, int targetPos)
     
    {
       int error = targetPos - currentPos;
    }
    
    void loop() //This is the main
    {
      //calculateError(targetPos,currentPos); //error when this is used, guess it is wrong.
    }

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    That's because targetPos and currentPos don't exist outside the scope of their functions. You have to call the functions and pass their return values as arguments to calculateError.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Subroutines or maybe functions?
    By Gutiarplyrinak in forum C Programming
    Replies: 5
    Last Post: 12-03-2010, 05:08 PM
  2. Does C Support Subroutines
    By AQWst in forum C Programming
    Replies: 7
    Last Post: 12-10-2004, 11:13 PM
  3. calling subroutines to populate array??help
    By gravitybass in forum C Programming
    Replies: 1
    Last Post: 04-07-2003, 12:23 AM
  4. Subroutines
    By drdroid33 in forum C++ Programming
    Replies: 3
    Last Post: 02-02-2002, 07:48 PM
  5. Subroutines
    By drdroid33 in forum C++ Programming
    Replies: 1
    Last Post: 02-02-2002, 07:01 PM