Hello Recursion!

Since I haven’t posted since last year :/ I figured why not show some recursion?

I know a lot of people try out something like a Fibonacci Sequence to help grasp the paradigm of recursion. I am going to do something a bit simpler. We will adapt the “Hello, World!” into a recursive function. We will play a bit with stdout in python as well.

First, here is some code:

import sys

def herro(st, cnt):
    if cnt < len(st):
        sys.stdout.write(st[cnt])
        cnt += 1
        herro(st, cnt)

    return

herro('Hello, World!\n', 0)

First, we import sys so we can use stdout. Then we define our function that shall call itself. We have two parameters, one for the string itself, and one to check for recursion depth. If we didn’t have the “if” statement in there it would call itself forever and end up running out of memory. We then use stdout to write directly to the shell. We do this because print() would append a newline to each iteration of the function. Add one to cnt so we don’t go further than the length of string we’re printing. At the end, return nothing to exit the function.

You might be wondering why I added a newline char (\n) to the end of the string we supplied. I did that for formatting only. If there was no newline at the end, we would see the results before the prompt and it doesn’t look too well. Go ahead and remove the newline to see what happens.

About these ads

About Tech B.

I am currently employed as a tech support rep for Frontier DSL internet and Dail-Up service. If you live on the East Coast and have Frontier or the old Verizon, you may have spoken with me at one point. I do side jobs programming and building things for people. I know Python better than any language in my toolbox. I can also develop Android Apps, which are Java based. Other languages include C , VB/VB.NET, some C#, PBASIC, Batch, Javascript, and some PHP. I love microcontrollers and interfacing with the outside world. I am currently working with Arduinos, which are amazing. Also I can work with the Basic Stamp family of microcontrollers; future development includes FPGA and embedded Linux. I was going to school, but have financial issues at the moment that are keeping me from the books. I plan on going back in the next couple semesters and finishing up my Associates in IT, then perusing a BS in computer science.
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Hello Recursion!

  1. Traditional way in recursion is not to have loop counter, but have something like:

    `

    import sys
    
    def herro(st):
        if len(st):
            sys.stdout.write(st[0])
            herro(st[1:])
    
    herro('Hello, World!\n')

    `

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s