DOC PREVIEW
Rutgers University CS 105 - Subroutines & Recursion

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 6:Subroutines & RecursionCS105: Great Insights in Computer ScienceMichael L. Littman, Fall 2006Recap: ReductionLevelExamplesAlternativessoftware librariesgraphics, animation, roboticsnetworking, security, mathematicshigh-level languagePythonC, Java, C++, Logo, LISP, Fortran, MLmachine languageML3x86, CARDIAC, Z80logic gatesequal, ifthenelse, addmemlookup, memwritebasic logic gatesand, or, notnor, nand, xor physical bits0,1 via high/low voltagewater pressure, kinetic energyDoes It End There?• Of course, not.• We can continue to build sophisticated programs out of simpler programs.• The idea of subroutines (procedures, functions) makes this work.Subroutines• A lot of research in computer science is about designing and creating just the right set of subroutines, sometimes called libraries.• You don’t have enough background yet to weigh in on these problems.• But, there is an analogous set of problems where you are already an expert...Sing-A-Long ProgramsGilligan’s Island ThemeJust sit right back and you'll hear a tale, a tale of a fateful trip.That started from this tropic port, aboard this tiny ship.The mate was a mighty sailin' man, the skipper brave and sure.Five passengers set sail that day, for a three hour tour, a three hour tour…The weather started getting rough, the tiny ship was tossed.If not for the courage of the fearless crew,the Minnow would be lost; the Minnow would be lost.The ship took ground on the shore of this uncharted desert isle,with Gilligan, the Skipper too, the Millionaire, and his Wife,the Movie Star, the Professor and Mary Ann, here on Gilligan's Isle.Python Codedef GilligansIsland():print “Just sit right back and you'll hear a tale, a tale of a fateful trip.”print “That started from this tropic port, aboard this tiny ship.”print “The mate was a mighty sailin' man, the skipper brave and sure.”print “Five passengers set sail that day, for a three hour tour, a three hour tour.”print “The weather started getting rough, the tiny ship was tossed.”print “If not for the courage of the fearless crew,”print “ the Minnow would be lost; the Minnow would be lost.”print “The ship took ground on the shore of this uncharted desert isle,”print “ with Gilligan, the Skipper too, the Millionaire, and his Wife,”print “the Movie Star, the Professor and Mary Ann, here on Gilligan's Isle.”GilligansIsland()Chorus StructureClementineIn a cavern, in a canyon,Excavating for a mine,Dwelt a miner forty niner,And his daughter Clementine.Oh my darling, oh my darling,Oh my darling, Clementine!Thou art lost and gone foreverDreadful sorry, Clementine.Light she was and like a fairy,And her shoes were number nine,Herring boxes, without topses,Sandals were for Clementine.Oh my darling, oh my darling,Oh my darling, Clementine!Thou art lost and gone foreverDreadful sorry, ClementineDrove she ducklings to the waterEv'ry morning just at nine,Hit her foot against a splinter,Fell into the foaming brine.Oh my darling, oh my darling,Oh my darling, Clementine!Thou art lost and gone foreverDreadful sorry, Clementine.Ruby lips above the water,Blowing bubbles, soft and fine,But, alas, I was no swimmer,So I lost my Clementine.Oh my darling, oh my darling,Oh my darling, Clementine!Thou art lost and gone foreverDreadful sorry, Clementine.How I missed her! How I missed her,How I missed my Clementine,But I kissed her little sister,I forgot my Clementine.Oh my darling, oh my darling,Oh my darling, Clementine!Thou art lost and gone foreverDreadful sorry, Clementine.Python Codedef Clementine():print "In a cavern, in a canyon,"print "Excavating for a mine,"print "Dwelt a miner forty niner,"print "And his daughter Clementine."printprint "Oh my darling, oh my darling,"print "Oh my darling, Clementine!"print "Thou art lost and gone forever"print "Dreadful sorry, Clementine."printprint "Light she was and like a fairy,"print "And her shoes were number nine,"print "Herring boxes, without topses,"print "Sandals were for Clementine."printprint "Oh my darling, oh my darling,"print "Oh my darling, Clementine!"print "Thou art lost and gone forever"print "Dreadful sorry, Clementine."printprint "Drove she ducklings to the water"print "Ev'ry morning just at nine,"print "Hit her foot against a splinter,"print "Fell into the foaming brine."printprint "Oh my darling, oh my darling,"print "Oh my darling, Clementine!"print "Thou art lost and gone forever"print "Dreadful sorry, Clementine."printprint "Ruby lips above the water,"print "Blowing bubbles, soft and fine,"print "But, alas, I was no swimmer,"print "So I lost my Clementine."printprint "Oh my darling, oh my darling,"print "Oh my darling, Clementine!"print "Thou art lost and gone forever"print "Dreadful sorry, Clementine."printprint "How I missed her! How I missed her,"print "How I missed my Clementine,"print "But I kissed her little sister,"print "I forgot my Clementine."printprint "Oh my darling, oh my darling,"print "Oh my darling, Clementine!"print "Thou art lost and gone forever"print "Dreadful sorry, Clementine."Clementine()def ClementineChorus()printprint "Oh my darling, oh my darling,"print "Oh my darling, Clementine!"print "Thou art lost and gone forever"print "Dreadful sorry, Clementine."Using A Subroutinedef ClementineChorus()printprint "Oh my darling, oh my darling,"print "Oh my darling, Clementine!"print "Thou art lost and gone forever"print "Dreadful sorry, Clementine."def Clementine():print "In a cavern, in a canyon,"print "Excavating for a mine,"print "Dwelt a miner forty niner,"print "And his daughter Clementine."ClementineChorus()printprint "Light she was and like a fairy,"print "And her shoes were number nine,"print "Herring boxes, without topses,"print "Sandals were for Clementine."ClementineChorus()printprint "Drove she ducklings to the water"print "Ev'ry morning just at nine,"print "Hit her foot against a splinter,"print "Fell into the foaming brine."ClementineChorus()printprint "Ruby lips above the water,"print "Blowing bubbles, soft and fine,"print "But, alas, I was no swimmer,"print "So I lost my Clementine."ClementineChorus()printprint "How I missed her! How I missed her,"print "How I missed my Clementine,"print "But I kissed her little sister,"print "I forgot my Clementine."ClementineChorus()Clementine()• Now, “ClementineChorus()” is its own little program, or a new statement in the language, that we can call whenever we need it.• Calling the subroutine does all the


View Full Document

Rutgers University CS 105 - Subroutines & Recursion

Download Subroutines & Recursion
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Subroutines & Recursion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Subroutines & Recursion 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?