recursion with or without return?

N

notnorwegian

when using recursion should one use a return statement or not?

there is a difference obv since with a return statement it will
ultimately return a value if not recursing forever.

but is there a guideline for this or it just taste or is it
considering good style or pythonic to always have a returnvalue?
 
M

Marc 'BlackJack' Rintsch

when using recursion should one use a return statement or not?

This decision has nothing to do with recursion. It's the same as in
non recursive functions. If the function calculates something that you
want to return to the caller you have to use ``return``. If the function
just has side effects, e.g. printing a tree structure recursively, you
don't have to ``return`` something.
but is there a guideline for this or it just taste or is it
considering good style or pythonic to always have a returnvalue?

Well, you always have a return value anyway because there's an implicit
``return None`` at the end of every function.

Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top