Read sections 2.1-2.4 in chapter 2 of the textbook Explorations in Computing and read pages 19-42 of chapter 2 of the book Blown To Bits.
3 * 4 + 5 12 + 5 17HINT: You can check your final answers with irb!!!
def compute_frequency(base, n) # computes the frequency of a note n semitones above the base frequency # in a well-tempered scale return base * 2 ** (n/12.0) end
compute_frequency(440, 12)
Will we get an integer result or a floating point result? Why?
compute_frequency(440)
What error does Ruby give?
compute_frequency(12,440)
Does Ruby report an error? Why or why not?
def print_frequency(base, n) # computes the frequence of a note n semitones above the base frequency # in a well-tempered scale print base * 2 ** (n/12.0) end
What value is stored in the variable note if we execute the following instruction? Why?
note = print_frequency(440, 5)
def mystery(n) value = 0 for i in 1..n do value = value + 3 print value print "\n" # print a newline character end end
mystery(10)
value = value * 3
What does this revised method display if we call it as follows:
mystery(10)
def mystery(n) value = 1 for i in 1..n do value = value + 3 return value end end
Store the function in a file, then load it in irb and call it with different positive integers and observe the results. What do your observations suggest about how the return statement works?