Lab 3, Part 1: Parameter practice

Write the code for this part in a new file, lab03/parameters.py.

Task 0. rocks: a zero-parameter function

Write a None function called rocks that prints cs111 rocks! when invoked. This is called a zero-parameter function because it does not have any parameters. Even without any parameters, you still need the parentheses () when invoking the function.

>>> rocks()
  cs111 rocks! 

Task 1. bunny: a zero-parameter function

Write a None function called bunny that prints a multi-line bunny when invoked.

>>> bunny()
  ()()
  (-.-)
  o_(")(")

Task 2. Single Parameter Functions

Task 2A.customRocks: a single parameter function

Write a None function called customRocks that prints a customized message using the supplied argument.

>>> customRocks('olivia')
  olivia rocks!

>>> customRocks('wellesley')
  wellesley rocks!

>>> customRocks('peppa the pig')
  peppa the pig rocks!

Task 2B. diff21: how far from 21

Write a None function called diff21 that prints the positive difference between the given number and 21. Hint: use python's built-in abs() function which returns the absolute value of a number.

>>> diff21(10)
  11
>>> diff21(27)
  6
>>> diff21(21)
  0

Task 2C. banner

Write a None function called banner that takes a string parameter and prints two lines: the first line as 5 stars before the string and after the string, with one space on either side of the string, and the second line has 6 stars before the string and 4 stars afterward. (see examples below)

>>> banner('hello')
 ***** hello *****
 ****** hello ****

>>> banner('butter popcorn')
 ***** butter popcorn *****
 ****** butter popcorn ****

You should test your banner function to make sure it works with any parameter (some examples shown below):

>>> banner('YO')
 ***** YO *****
 ****** YO ****

>>> banner('Come and play with us')
 ***** Come and play with us *****
 ****** Come and play with us ****

Task 3. Functions with more than one parameter

Hint: For some of the tasks below, you can use python's built-in abs() function which returns the absolute value of a given number.

Task 3A. customRocksRepeat: a multiple parameter function

Write a None function called customRocksRepeat that accepts two parameters: message and n

This function should print the message repeated by n times. Note: scroll right in the box below to see all the output

>>> customRocksRepeat('olivia', 1)
  olivia rocks!

>>> customRocksRepeat('wellesley', 2)
  wellesley rocks! wellesley rocks!

>>> customRocksRepeat('peppa the pig', 5)
  peppa the pig rocks! peppa the pig rocks! peppa the pig rocks! peppa the pig rocks! peppa the pig rocks!

>>> customRocksRepeat('yo', 10)
  yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! yo rocks! 
Peppa the Pig: A cheery cartoon pig wearing an orange shirt and black shoes.

Task 3B. diff:

Write a None function called diff that accepts two numbers and prints the positive difference between them.

>>> diff(5,21)
  16
>>> diff(20,17)
  3
>>> diff(100,-19)
  119
>>> diff(4,4)
  0

Task 3C. fruityDiff:

Write a FRUITFUL function called fruityDiff that accepts two numbers and returns the positive difference between them.
Note: Thonny's shell displays the results of fruitful functions in blue, left-aligned text. (but not with a None function). Contrast the sample output below with the sample output above in Task 3B.

>>> fruityDiff(5,21)
16
>>> fruityDiff(20,17)
3
>>> fruityDiff(100,-19)
119
>>> fruityDiff(4,4)
0

Task 3D. multDiff:

Write a FRUITFUL function called multDiff that accepts three numbers and returns the product of the positive difference between the first and second numbers and the positive difference between the second and third numbers. Hint: multDiff can invoke fruityDiff.

Note: Thonny's shell displays the result of a fruitful function left-aligned and in blue. It's a subtle difference, but note how Thonny displays the side effect of a None function (diff) versus the returned value of a fruitful function (fruityDiff):

The difference between a side effect and a returned value in the Thonny shell is subtle. A side effect is shown in slightly smaller black text, while a returned value is shown in a larger font and is colored according to its type (dark blue for numbers in this case). In some other consoles, there is no visual difference between what is printed after a statement like `>>> print(55)` and an expression like `>>> 55`, so one would have to remember whether an output is a side effect or a result based on what statement/expression they wrote.
Detailed rules about what is displayed Although this can be confusing, the rules for what is displayed in the shell when you evaluate an expression are straightforward: Because the print function returns the special value None, if you just call print, only the printed output will be displayed, because of the rules above. Similarly, if you call a non-fruitful function, only what it prints will be displayed, while if you call a fruitful function, first whatever it prints will be displayed, followed by its return value (because the value of a function call as an expression is the return value of that function). The two rules above only apply to code types in the shell. For code in a file, the second rule is skipped. Finally, the way in which returned values are displayed is different from printed values: their representations are displayed instead of their string equivalent. This is most obvious when it comes to values which are strings already: when printed, their characters will appear as-is, but when displayed as a result value, quotation marks will appear to indicate that the value is a string. Notice the difference:
>>> print("hello")
hello
>>> "hell" + "o"
'hello'

Some examples for multiDiff:

>>> multDiff(1,5,10) # note: 4 * 5 = 20
20 
>>> multDiff(8,2,4)
?? What do you expect? 
>>> multDiff(10,-2,5)
??

Table of Contents