Introduction To Programming: Tutorial Task 2.2: Using Functions (Hello User With Functions)
Introduction To Programming: Tutorial Task 2.2: Using Functions (Hello User With Functions)
Introduction to Programming
Tutorial Task 2.2: Using Functions (Hello User with Functions)
Overview
In this task we change our Reading and Writing (Hello User) program to use functions to
prompt and read in values.
Purpose: Learn how to use functions.
Task: Use the instructions on the following pages to modify your own Hello
User program from Pass Task 1.3. Submit to Doubtfire when complete.
Time: This task should be completed in your lab class.
Resources: ■ Chapters 1 – 3 of ‘Learn to Program’
Chapt 1: Numbers
Chapt 2: Letters
Chapt 4: Mixing it up
Submission Details
You must submit the following files to Doubtfire:
■ Modified Reading and Writing (Hello User) source code (hello_user_with_functions.rb)
■ Screenshot of the Terminal showing the execution of your modified Hello User program.
Make sure that your task has the following in your submission:
■ The program must use Ruby functions provided in input_functions.rb to read the required details from
Instructions
Functions allow you to ‘chunk’ together related lines of code. Functions can receive data as
arguments and allow you to return a value.
The functions we are going to use in this task are contained in the file input_functions.rb (see
the resources for this task). We are going to modify our previous program to use these
functions.
If you recall our Reading and Writing (Hello User) program did the following:
■ ask the user to enter their name, age, and the current year,
■ calculate the year the user was born, and
■ output a greeting message.
1. Download the input_functions.rb Ruby code from the Doubtfire (see the
Resources for this task). This contains some useful functions you can use to read
values from the user.
2. Copy input_functions.rb into the folder next to your hello_user.rb file (from Pass Task
1.3).
3. Using Atom or VS Code (or similar) declare the hello_user_with_functions program
based on the program start code (hello_user_with_functions_incomplete.rb) shown
below (also in the Resources for the task).
require 'date'
require './input_functions'
def main()
# Copy in your code from your completed
# hello_user Task 1.3 P. Then modify it to
# use the code in input_functions.
# use read_string for all strings (this will
# remove all whitespace)
end
main()
Introduction to Programming Tutorial Task 2.2: Using Functions
1. Read the user's name (a String, prompt with 'Please enter your
name: ') and store it in the name variable.
2. Print out the user’s name followed by an exclamation mark.
3. Read the user's family name and store it in the family_name
variable.
4. Print out the user’s family_name followed by an exclamation
mark.
5. Read in the user’s year of birth as an integer
6. Calculate the user’s age and print it out.
7. Prompt for and read in the user’s height in metres as a float
8. Convert the height to inches and print out the result. Use the
constant INCHES at the top of the code to convert metres to
inches
9. Call the read_boolean() function from input_functions.rb
passing in the argument “Do you want to continue?”
10. If the read_boolean() function returns true then display “ok,
lets continue”, if the function returns false then output “ok,
goodbye”.
Items 9 and 10 should look as follows:
5. Use Ruby to compile and run your program. An example of it running is as follows:
Introduction to Programming Tutorial Task 2.2: Using Functions
Now that the Task is complete you can submit it for assessment, which will help prepare it for
your portfolio.
1. Use Skitch (or your preferred screenshot program) to take a screenshot of the
Terminal, as this is one of the things you will need to submit.
2. Save the document and backup your work to multiple locations!
• Once you get things working you do not want to lose them.
• Work on your computer’s storage device most of the time... but backup your
work when you finish each task.
• Use Dropbox or a similar online storage provider, as well as other locations.
• Doubtfire is not a Backup of your work, so make sure you keep a copy!
• A USB key and portable hard drives are good secondary backups... but can be lost/
damaged (do not rely upon them).
3. Login to Doubtfire, and locate Tutorial Task 2.2
4. Change the status of the task to Ready To Mark
5. Upload your completed Using Functions code and the screenshot.
6. If you check back later Doubtfire will have prepared these as PDFs for your tutor to
assess.
Note: This is one of the tasks you need to submit to Doubtfire. Tutors should give
guidance and perhaps feedback in the tutorial class.
Check the assessment criteria for the important aspect your tutor MAY check when
assessing your finished portfolio.
End of Task