Getting Started

(lily) #1

Chapter 6: C Functions and Program Structures


int b = x + y + z – 12;
// do stuff

en a function calls itself. I’ve never called myself. I’m
en have to deal with the philosophical
s no problems with functions calling
themselves, other than the psychiatric problems it tends to cause programmers
when confronted with a recursive function and the task of figuring out what’s
going on. C Gurus love recursive functions.


unc(double data)


double ss;
e stuff
recursivefunc(mess);


very problematic in
limited in RAM memory. Each time you
stack using RAM that isn’t released until
ch function you call within a function puts more data on
more RAM. Recursive functions look like a good way
ably fill the stack leading to the often-fatal condition known
lly starts at the end of RAM and builds
start at the beginning of RAM and build
e stack may push it down far enough overwrite
mediately or only occasionally, like at the
ever written
or the same

int dosomething(int x, int y, int z)
{
int a = 0;


}


Recursion


Recursion happens wh
afraid that I might answer the phone and th
or psychiatric implications. However, C ha


void recursivef
{
me
// Do som

// Do more stuff
}


You’ll find recursion used quite appropriately in some standard library functions
and in many data sorting applications. But recursion can be
microcontrollers where we are usually
call a function some data is put on the
the function returns. Ea
the stack locking up
quickly and unpredict
as blowing your stack. Your stack usua
downward. Your variables usuall
on th


y
upward. So putting too much
variables. That may kill your code im
worse possible moment. Maybe I’m just not smart enough, but I’ve n
a recursive function for a microcontroller and I don’t plan on it. F
reason, I also try not to nest function calls too deep.

Free download pdf