Thread: Not sure the right wording but can I DEFINE an entire block of code with one Name?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    28

    Not sure the right wording but can I DEFINE an entire block of code with one Name?

    I have a program with some pretty repetitive blocks of code (i.e Loop A, Loop B, Loop C, then Loop A again... etc) , is it possible to define an entire loop as a single line/function?

    Say I have this:
    Code:
    { 
      for(pos = 0; pos < 180; pos += 1)  // goes from 0 degrees to 180 degrees 
      {                                  // in steps of 1 degree 
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(15);                       // waits 15ms for the servo to reach the position 
      } 
      for(pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees 
      {                                
        myservo.write(pos);              // tell servo to go to position in variable 'pos' 
        delay(15);                       // waits 15ms for the servo to reach the position 
      } 
    }
    Could I abbreviate that in some way so that I can call on it with a single line of code? i.e. Variable X=all of that in the code bracket, and if I want that to run I can just type an X in my main loop? I think that'd be defining a function but I'm not sure how to impliment it.

    I've got a very messy program with lots of loops like the above that I'd like to neaten up a bit. Thanks for any advice/tips.
    Last edited by 777funk; 10-05-2012 at 09:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code::Block Land
    By imtithal in forum C++ Programming
    Replies: 2
    Last Post: 04-07-2011, 12:23 PM
  2. Can't understand this block of code
    By serg_yegi in forum C Programming
    Replies: 1
    Last Post: 03-23-2010, 06:58 PM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. Code::Block question
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2007, 01:14 AM
  5. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM