Learn Ruby the Hard Way – Exercise 21 Functions can Return Something

Here is my work for Exercise 21. (Hey, getting closer to the halfway point!)

Exercise 21 code
Exercise 21 code
Exercise 21 output
Exercise 21 output

Study Drills
1. If you aren’t really sure what return does, try writing a few of your own functions and have them return some values. You can return anything that you can put to the right of an =.
When a function is called and the code reaches a return statement, the function execution is ended and a value is returned to the function caller. For example on line 24, we assign the returned value from the function call to a variable called age. The value returned from add(30, 5) is 5, so the integer value of 5 is assigned to the age variable.

2. At the end of the script is a puzzle. I’m taking the return value of one function and using it as the argument of another function. I’m doing this in a chain so that I’m kind of creating a formula using the functions. It looks really weird, but if you run the script you can see the results. What you should do is try to figure out the normal formula that would recreate this same set of operations.
The math formula to recreate the operation is age + (height – ( iq / 2) * weight)

If we see what is happening with the variables (analyzing what happens with the function calls for these variables), we will get the formula below.
35 + (74 – (50 / 2) * (180) )
We can simplify this to:
35 + (74 – (25) * (180))
then:
35 + (74 – 4500)
and finally we get:
35 + (-4426)
and the answer is:
– 4426! Woohoo!

3. Once you have the formula worked out for the puzzle, get in there and see what happens when you modify the parts of the functions. Try to change it on purpose to make another value.
I changed the order of the arguments entered into the subtract function.
The formula becomes age + (( iq / 2) * weight – height).
We get 35 + ((50/2) * 180 – 74), which simplifies to 35 + ((25 * 180) – 74).
We can simplify further to 35 + (4500 – 74). Finally we get, 35 + 4426, which equals 4461.

Let’s check our math with our program!

Let's switch the order of the values we subtract!
Let’s switch the order of the values we subtract!
Yay, we get 4461!
Yay, we get 4461!

We get 4461, as we predicted from our math calculations.

4. Do the inverse. Write a simple formula and use the functions in the same way to calculate it.
My math formula: (((iq + 10) – 30) * 2) / 3
If we calculate this by hand, we get( ((50 + 10) – 30) * 2)/ 3, which we can simplify to (60 – 30) * 2 / 3, and further into 30 * 2 / 3, which should be 20! Let’s see if our script gets the same result.

Our code
Our code
Success, the answer is 20!
Success, the answer is 20!

5. Remove the word return and see if the script still works. You’ll find that it does because Ruby implicitly returns whatever the last expression calculates. However, this isn’t clear so I want you to do it explicitly for my book.
Ok, boss!

Summary
This exercise is all about learning how functions can return values. We can use these returned values as arguments to call other functions (or the function itself again!). We can call functions, then use the returned values as arguments, to “combine” several functions to do our math calculations.

 

Leave a comment