Thread: Would this be considered defining a funcion?

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

    Would this be considered defining a funcion?

    I'd like to run a block of code interspersed throughout a program and rather than making a mess in the program I'd like to define it as a variable. I'm not sure if that's the right terminology but I'd like to represent

    Code:
      // read the state of the Relay value:
      buttonState = digitalRead(buttonPin);
    
      // check if the Relay is on.
      // if it is, the buttonState is HIGH:
    if (buttonState != HIGH) 
    { 
    break;
    }    
        // exit program
    with a single word/variable etc. Is that possible? It'd make my program much cleaner and easier to follow if I don't have to tack this mess in everywhere I need it.

    thanks in advance for any advice!!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well one thing you could do is move the functionality in a function and return a bool on whether the "relay is ON or OFF" ?!

    Is that what you are looking for?
    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
    Nov 2010
    Posts
    28
    It is a check for the relay on or off and an exit if it is OFF.

    But I'd like to abbreviate it a bit or represent it as a variable if it's possible.

    i.e.

    getout = that entire block of code
    Last edited by 777funk; 12-08-2010 at 05:40 PM.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Is there no way to define an entire block of code as a command?

    Sorry if this is a dumb question. I'm new to C.

    Once again I'd like to condense a group of code into a single function. I'm a newby and unfamiliar with how to do this.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So have you read the tutorials on this site about functions? Have you looked at any of the threads about functions on this message board? What's stopping you from doing so?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by 777funk View Post
    I'd like to run a block of code interspersed throughout a program and rather than making a mess in the program I'd like to define it as a variable.
    Code:
      // check if the Relay is on.
      // if it is, the buttonState is HIGH:
    
    if (digitalread(buttonPin) != HIGH) 
    { 
    break;
    }    
        // exit program
    See... no problem.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Quote Originally Posted by tabstop View Post
    So have you read the tutorials on this site about functions? Have you looked at any of the threads about functions on this message board? What's stopping you from doing so?
    That's quite a welcome! Thank you. Wow.

    As far as reading, I've done some (Ritchie's book and a few others). I will have to do more. I am learning C and am in a rush on a quick project. I figured this would be the place to get zeroed in on a specific area to research just what I need. I have a lot of irons in the fire with a business I'm trying to maintain right now and haven't had tons of time to learn C other than the bare essentials.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining matricies
    By rich999 in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2005, 07:59 PM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Defining Class memeber functions
    By silk.odyssey in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 05:50 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM