Computer Science Canada

Mad Libs using Procedures

Author:  turwaa [ Sun Apr 22, 2012 11:59 am ]
Post subject:  Mad Libs using Procedures

i have to create a Mad Libs game using procedures to make the program shorter and easier to make
can anybody help me find out how to use the procedures appropriately and effectively? thank you

Author:  DemonWasp [ Sun Apr 22, 2012 1:31 pm ]
Post subject:  RE:Mad Libs using Procedures

Creating a procedure is a lot like giving a name to a few lines of code. For example, procedure get_coffee() might read:
1. Walk to Timmy's.
2. Wait in line.
3. Purchase coffee.

Of course, that implies procedure walk_to ( location ), procedure wait_in_line() and purchase ( item ).

A "function" in Turing is just a special case of a procedure that returns a value. For example, round() takes a real number and converts it to an integer, which it then returns.

One of the simplest, most straightforward procedures you will find useful to write for yourself is a safe-input procedure. Your assignments will often require that the user input an integer, and that if they input something wrong, then instead of crashing your program should give them another chance. You can wrap all that logic up into a function pretty easily.

I would suggest that the first line of that function be:
function prompt_user_int ( question : string ) : int

You can fill in the rest. Don't forget to use result to return the user's (correct) input.

Once you have that written, then whenever you need the user to input an integer, you can just call:
code:

var cats : int := prompt_user_int ( "How many cats? " )
% or
var year : int := prompt_user_int ( "In what year were you born? " )

Author:  Raknarg [ Sun Apr 22, 2012 2:02 pm ]
Post subject:  RE:Mad Libs using Procedures

You can look at the Turing Walkthrough for more help


: