0

I have a conceptual doubt about using recursion in python. Below is the python code for reversing a stack using only recursion which I have copied from this page at geeksforgeeks.

# Below is a recursive function that inserts an element
# at the bottom of a stack.
def insertAtBottom(stack, item):
 if isEmpty(stack):
 push(stack, item)
 else:
 temp = pop(stack)
 insertAtBottom(stack, item)
 push(stack, temp)
# Below is the function that reverses the given stack
# using insertAtBottom()
def reverse(stack):
 if not isEmpty(stack):
 temp = pop(stack)
 reverse(stack)
 insertAtBottom(stack, temp)

It seems like the function reverse is using stack as a global variable because the called function isn't returning any new value to the caller function. Isn't this a wrong way of implementing recursion? Shouldn't we avoid using global variables in stack?

Also, how would we edit this function such that each instance of the called function uses it's own copy of stack?

asked Mar 7, 2017 at 10:21
2
  • Please try to break your sentence down "What I wish ... function". This is a headache.. Commented Mar 7, 2017 at 10:24
  • stack is not global, it's being passed into the function. Commented Mar 7, 2017 at 10:29

3 Answers 3

1

Edit: reworking the phrasing to be more consistent with the terms used.

stack is not a global variable, it's a parameter of the function. You can imagine it as a reference to the stack object instance that was created somewhere else before calling the function. Operating on the parameter actually modifies the object referenced by it.

answered Mar 7, 2017 at 10:35

3 Comments

You have a lot of similar terms, variable, parameter, reference, but you're using them quite inconsistently, even incorrectly.
Please elaborate on your comment. I updated my answer to make it clearer and more consistent with the terms, but I can't find any point in which I use the terms incorrectly.
One problem for me was saying stack wasn't a variable, but was a parameter. Now you're making the distinction to do with scope, much better. Also you said operating on the parameter modifies the original variable, which is confusing. In some ways Python doesn't have variables. Names reference objects. You can change a name to reference a different object, or you can modify the object. So I like that you now say it modifies the object referenced by the parameter.
0

push and pop will be modifying the object which stack is a name for. Everywhere stack is mentioned it is a name for the same object, because it is passed to reverse and insertAtBottom as a parameter named stack. No objects are copied, and neither are they global. You could name your parameters something different but it would still refer to the same object.

Whatever you pass into the reverse function will be used. It's not global, you have control over which object is used.

my_stack = []
reverse(my_stack) # uses object *my_stack*
answered Mar 7, 2017 at 10:35

Comments

0

Function calling itself is known as recursive function and this process is known as Recursion .For More detail this can help

answered May 13, 2019 at 7:13

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.