can you use a loop inside a function or an if statement- mikroc

Discussion in 'Programmer's Corner' started by rocker123uk, Mar 10, 2016.

  1. rocker123uk

    Thread Starter New Member

    Dec 6, 2015
    10
    0
    hi guys,

    just a quick question, am i able to use an if statement or a while loop inside a function or will the microcontroller get confused?
    for example:
    Code (C):
    1.  
    2. void function() {
    3. while(1){
    4.   differentfunction();
    5.   step--;
    6.   if(step==0)
    7.     reset();
    8.  }
    9. }
    Mod edit: code tags
     
    Last edited by a moderator: Mar 10, 2016
  2. WBahn

    Moderator

    Mar 31, 2012
    17,777
    4,805
    Since all executable statements have to be inside functions, where else would you put them?
     
    rocker123uk likes this.
  3. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,067
    3,837
    Yes
     
    rocker123uk likes this.
  4. WBahn

    Moderator

    Mar 31, 2012
    17,777
    4,805
    One practical recommendation:

    When possible, don't write code like this:

    if(step==0)

    Instead, write it like this:

    if (0 == step)

    The reason is that a very common mistake even experienced programmers make from time to time is to use '=' instead of '=='. If you make that mistake, the first statement becomes

    if(step=0)

    which is perfectly valid C code. When run, it will set step equal to zero and fail the test and then proceed on. This can be a very challenging logic error to find because the human brain will tend to see what was meant and not what was written. However, if you make the same mistake with the second form you have

    if (0 = step)

    which is a syntax error. Always try to adopt coding practices that turn common logic errors into syntax errors that the compiler can fail on immediately.
     
  5. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,607
    And to add to WBahns good comment don't write code like

    if(0==step)

    because the "0" has little meaning. Use either a #define or better an enumeration to give your magic numbers sensable names a human can understand.

    #define BEGIN_TASK 0
    ...
    if (BEGIN_TASK ==step)

    Has much more meaning.

    Aside: while an assignment inside an if statement is 100% legal I truly wish a compiler warning would be emitter when such is detected. The times this is desired is far outweighed by the times it is an error.
     
    anhnha and Robin Mitchell like this.
  6. vpoko

    Member

    Jan 5, 2012
    258
    47
    In GNU C, if you use the -Wparentheses flag (or -Wall), it will throw a warning if it looks like you're using an assignment where a comparison would typically go.
     
    ErnieM likes this.
  7. WBahn

    Moderator

    Mar 31, 2012
    17,777
    4,805
    Many compilers have gotten pretty good about issuing these kinds of warnings. There are usually arcane compiler switches you can use to turn them on or off.
     
  8. BobaMosfet

    Active Member

    Jul 1, 2009
    109
    11
    Just a note-- if it can return a value, it's a function. If it can't, it's a procedure. And yes, of course, you maybe put any syntacticly legal statements within either a function or a procedure. Everything else has to be either a declarative (global variables, defines, includes, macros, etc, or something that controls compilation)
     
  9. WBahn

    Moderator

    Mar 31, 2012
    17,777
    4,805
    Note that many languages won't let you do that (but C will). Using that shortcut has both pros and cons (what doesn't). The con is that it reduces the readability of the code since using a Boolean operator to check for a numeric value is only possible due to a quirk of the language definition and is not a good match to most problem logic. But there are times when it is a decent enough match that it is widely used, such as checking for NULL pointers.
     
  10. WBahn

    Moderator

    Mar 31, 2012
    17,777
    4,805
    This is a distinction that C does not make. In the Pascal-like languages, a function must be used as part of a statement and cannot be a statement by itself, hence the necessary distinction between it and a procedure, which is used as a statement. In fact, the word "procedure" doesn't even appear in the C99 language standard (or at least the N869 draft that I work from).
     
  11. vpoko

    Member

    Jan 5, 2012
    258
    47
    You're right that C doesn't explicitly make the distinction, but it's an important distinction for the programmer to keep in mind. A function should return data and have no side effects, while a procedure should have side effects but not have any calculations unless they're only relevant to implementing the procedure. I see the lack of a sub/procedure keyword in C as part of its trend of reusing keywords as much as possible, but the void return type should really be seen as C syntax for declaring a procedure.
     
  12. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    2,912
    2,177
    All that is great and good but side-effects (especially when something interacts with the outside world) are just changes in state which is the point in most embedded programming. The Pure Function (procedures that only compute a result value in a mathematical sense) distinction is less useful as an abstraction of machine hardware when low-level programming is the objective and any procedure can pass pointers to modifiable objects to return data as in old man C.
     
    vpoko likes this.
  13. WBahn

    Moderator

    Mar 31, 2012
    17,777
    4,805
    Although it is changing as more processing power is available in embedded systems, many embedded applications simply don't have the resources to afford a nice, high-level, ivory-tower level of abstraction. They have work to do and very limited time and memory with which to do it.
     
    vpoko likes this.
Loading...