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

#### rocker123uk

Joined Dec 6, 2015
33
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:
C:
void function() {
while(1){
differentfunction();
step--;
if(step==0)
reset();
}
}
Mod edit: code tags

Last edited by a moderator:

#### WBahn

Joined Mar 31, 2012
24,981
Since all executable statements have to be inside functions, where else would you put them?

#### GopherT

Joined Nov 23, 2012
8,012
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:
C:
void function() {
while(1){
differentfunction();
step--;
if(step==0)
reset();
}
}
Mod edit: code tags
Yes

#### WBahn

Joined Mar 31, 2012
24,981
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:
C:
void function() {
while(1){
differentfunction();
step--;
if(step==0)
reset();
}
}
Mod edit: code tags
One practical recommendation:

When possible, don't write code like this:

if(step==0)

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.

#### ErnieM

Joined Apr 24, 2011
8,010
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.

...

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.

#### vpoko

Joined Jan 5, 2012
267
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.
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.

#### WBahn

Joined Mar 31, 2012
24,981
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.

...

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.
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.

#### BobaMosfet

Joined Jul 1, 2009
893
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:
C:
void function() {
while(1){
differentfunction();
step--;
if(step==0)
reset();
}
}
Mod edit: code tags
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)

#### WBahn

Joined Mar 31, 2012
24,981
Actually, if you're going to compare anything to zero, then simply use the NOT operator:

if(!step)
{
...

You only care about zero or non-zero, so use '!'. Simpler, not possible to screw up with equals symbols. And you can check for either state:

If not zero = if(step)...
if zero = if(!step)...
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.

#### WBahn

Joined Mar 31, 2012
24,981
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)
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).

#### vpoko

Joined Jan 5, 2012
267
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).
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.

#### nsaspook

Joined Aug 27, 2009
6,610
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.
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.

#### WBahn

Joined Mar 31, 2012
24,981
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.