473,781 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1897
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
2294
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 class, as in: def walk(self): """old-style recursive tree traversal""" child.do_something for child in childs:
2
3430
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 is the recursive child of the first subsequent dep=n database call listed in the SQL data stream. The book gives a few examples, and in trying it out it seemed to work until I tried the following SQL. My question are why does this not keep with...
7
2658
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 the inorder traversal from http://www.python.org/peps/pep-0255.html as an example. def inorder(t): if t: for x in inorder(t.left):
4
9055
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. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
14
3483
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 of parent/child relationship. Now I would like to get all children of a given "Device" object and thought that it would be the best way to use recursive function.
41
3384
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 are that no local variable or global variable should be used.I have to use recursive functions.
5
4944
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 "inline". Is this a right answer? It seems to be independent of whether or not the function is recursive. Another question: how can you tell if the compiler has inlined your
2
2404
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 all the files and folders so that it would map the file system hierarchy.
10
1763
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 function in the first place)
10
1458
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 does it have anything to do with Ford cars either, but I somehow suspect that you did not ask in alt.autos.ford. Because recursive make is easy to do and understand and once someone had done it everyone else thought that it was the way to go?
0
9639
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8964
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7486
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.