473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Recursive question.

mdh
This little program, which is heavily adapted from K&R (Exc 4-12)
converts an integer to a string, recursively. My questions are these.

1) I think that as each successive recursion occurs, the automatic
variables are retained somewhere? so that when the function is
completed at a later point ( I hope I am expressing this vaguely
correctly) those automatic variables that were retained are again used.
Is my understanding correct?

2) In this particular function, I noticed that as each recursion
completes it's evaluation, 'k' is returned. I know where 'k' goes in
the last evaluation, (back to "main") but where does it go, for lack of
a better phrase, prior to that?

Thanks in advance.

/*****/
# include <stdio.h>
# define MAXNUM 50

int main () {
int itoa( char s[], int i);
int i=-234, j;
char s[MAXNUM];
j=itoa(s,i);
printf ("the number of characters in %s is %d", s, j);
return 0;
}
/******** itoa ***********/

# include <math.h>

int itoa( char s[MAXNUM], int i){
static int k;
{
if ( i / 10)
itoa(s, i/10);
else{
k=0;
if (i < 0)
s[k++]='-';
}
s[k++]=(abs(i) % 10 + '0');
s[k]='\0';
}

return k;

}

/******/

Oct 15 '06 #1
5 1876
mdh wrote:
This little program, which is heavily adapted from K&R (Exc 4-12)
converts an integer to a string, recursively. My questions are these.

1) I think that as each successive recursion occurs, the automatic
variables are retained somewhere? so that when the function is
completed at a later point ( I hope I am expressing this vaguely
correctly) those automatic variables that were retained are again used.
Is my understanding correct?
If I understand your explanation of your understanding correctly, then yes,
your understanding is correct.

Of course, it could be that I misunderstand your understanding.

:D
>
2) In this particular function, I noticed that as each recursion
completes it's evaluation, 'k' is returned. I know where 'k' goes in
the last evaluation, (back to "main") but where does it go, for lack of
a better phrase, prior to that?
It doesn't really "go" anywhere...it's ignored, because it's not needed
except when called from main(). The reason for this is that "k" is static,
which means that its lifetime is equal to the lifetime of the program,
rather than the lifetime of the particular instance of the particular
function call.

Think of a static local as essentially a global with restricted
scope--instead of being visible to the entire program or entire module,
it's only visible to the particular function it's called in. But, its
value is kept from one call of that function to the next--even if the
function is called recursively.

Here's an example:
int example(void){
int k = 0;

k++;
return k;
}

In this example, k will be reinitialized each time example() is called, so
example() will always return 1;

int example2(void){
static int k = 0;

k++;
return k;
}

In this example, k will only be initialized the first time example2() is
called, so the return value of the function will increase by 1 each time it
is called.

--
Kurt Weber
Help stop socialism: Vote Libertarian!
Oct 15 '06 #2
mdh wrote:
This little program, which is heavily adapted from K&R (Exc 4-12)
converts an integer to a string, recursively. My questions are these.

1) I think that as each successive recursion occurs, the automatic
variables are retained somewhere? so that when the function is
completed at a later point ( I hope I am expressing this vaguely
correctly) those automatic variables that were retained are again used.
Is my understanding correct?
Yes. Each invocation of a function gets its own private
set of `auto' variables, distinct from those of other active
functions. A function's own arguments behave the same way:
they act like `auto' variables that are initialized by the
function's caller, and each called function gets its own set.
2) In this particular function, I noticed that as each recursion
completes it's evaluation, 'k' is returned. I know where 'k' goes in
the last evaluation, (back to "main") but where does it go, for lack of
a better phrase, prior to that?
If a function returns a value, the caller has an opportunity
to make use of that value by storing it in a variable, adding it
to some other value, printing it out, or whatever. The caller
can also just ignore the returned value, in which case it "goes
nowhere" and simply "disappears." A caller is not obliged to use
the value a function returns. (Did you know that printf() returns
a value? Has non-use of that value ever bothered you?)
[code snipped; see up-thread]
--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 15 '06 #3
mdh

Kurt Weber wrote:
>If I understand your explanation of your understanding correctly, then yes,
your understanding is correct.

Of course, it could be that I misunderstand your understanding.

I understand!!! :-)

Thank you.

Oct 15 '06 #4
mdh

Eric Sosman wrote:
>Yes. Each invocation of a function gets its own private
set of `auto' variables, distinct from those of other active
functions. A function's own arguments behave the same way:
they act like `auto' variables that are in......

Thank you Eric

Oct 15 '06 #5
Kurt Weber wrote:
[snip]

The specs talked about a STRING, not a file.
Oct 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
2
by: Oxmard | last post by:
Armed with my new O'Reilly book Optimizing Oracle Performance I have been trying to get a better understanding of how Oracle works. The book makes the statement, " A database cal with dep=n + 1...
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
14
by: Fabian Steiner | last post by:
Hello! I have got a Python "Device" Object which has got a attribute (list) called children which my contain several other "Device" objects. I implemented it this way in order to achieve a kind...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
5
by: Digital Puer | last post by:
I got this on an interview: Is it possible to write inline recursive functions? I said yes, but there is no guarantee that the compiler will definitely inline your code even if you write...
2
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing...
10
by: pereges | last post by:
How to to go about this ? Suppose a malloc inside a recursive function has failed and you want to set the error flag and return it to the calling function(the one which called the recursive...
10
by: =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= | last post by:
On 2008-09-09 17:53, contactmayankjain@gmail.com wrote: And why did you pick this group, out of all possible groups, to ask this question? After all, it does not have anything to do with C++, nor...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.