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

This is a discussion on Not sure the right wording but can I DEFINE an entire block of code with one Name? within the C Programming forums, part of the General Programming Boards category; I have a program with some pretty repetitive blocks of code (i.e Loop A, Loop B, Loop C, then Loop ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    24

    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.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    Yes, such blocks of code are called functions.

    Eg.
    Code:
    void foo ( sometype myservo ) {
    int pos;
    { 
      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 
      } 
    }
    }
    You just look at whatever block of code you'd like to encapsulate, and figure out
    - what the "input" is (in this case, perhaps the servo to use)
    - what the "output" is (in this case, nothing)
    - what can be declared as a local variable (in this case, pos)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 03:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21