Recursive function to print a triangle

Hi everyone: I have a second recursive function written in Python and I’d like help with handtracing what it does. Here it is:

##
#  This program demonstrates how to print a triangle using recursion.
#

def main() :
   printTriangle(4)
   
## Prints a triangle with a given side length.
#  @param sideLength an integer indicating the length of the bottom row
#
def printTriangle(sideLength) :
   if sideLength < 1 : return
   printTriangle(sideLength - 1)
   
   # Print the row at the bottom.
   print("[]" * sideLength)
   
# Start the program.
main()


# Programme run 

[]
[][]
[][][]
[][][][]

I’d attempt this:

printTriangle 4 - 1 = 3
printTriangle 3 - 1 = 2

And they’re emptying something, but where it is going, when does it stop and why prints 1 first, 2 second, etc? Thanks in advance.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.