# Functions Practice Slides (Answers) 1. Write a function, last_letter, that takes a string as a parameter and returns the last character in the string Oops, I thought I had deleted this one... We just learned how to use indices for strings! You do not need to know how to do this, but here is the answer anyway: ```python=! def last_letter(s): # negative indices read from right to left return s[-1] ``` 2. Write a function, is_vowel, that takes a character as a parameter and returns a Boolean (True or False) if that character is a vowel or not. ```python=! def is_vowel(letter): if letter in "aeiouAEIOU": return True return False ``` 3. Write a function, vowel_count, that takes a string (could be a sentence) as a parameter and returns the number of vowels that occur in the string. Be sure to account for case. ```python=! def vowel_count(s): count = 0 for letter in s: if letter in "aeiouAEIOU": count += 1 return count ``` 4. Write a function, calculate_sum, that will take two numbers as parameters and returns the sum of those integers ```python=! def calculate_sum(num1, num2): return num1 + num2 ``` or ```python=! def calculate_sum(num1, num2): sum_val = num1 + num2 return sum_val ``` We can return the summation expression directly *or* store the sum as a variable and return that variable. 5. Write a function, calculate_avg, that will take two numbers as parameters and uses your `calculateSum` function to return the average of those two integers ```python=! def calculate_avg(num1, num2): return calculate_sum(num1, num2) / 2 ``` or ```python=! def calculate_avg(num1, num2): sum_val = calculate_sum(num1, num2) avg = sum_val / 2 return avg ``` Both do the same exact thing, but the first one does all three actions (finding sum of two numbers, finding average given the sum, and returning the average) in one line whereas the section example breaks it down into separate actions. 6. Write a function, max, that takes three numbers as parameters and returns the maximum of the three numbers. There are multiple ways to do this. Here is one of them: ```python= def max(num1, num2, num3): if num1 >= num2 and num1 >= num3: return num1 elif num2 >= num1 and num2 >= num3: return num2 else: return num3 ``` The logic: * line 2: compares num1 to num2 and num3. If num1 is greater or equal to num2 and num3, then num1 is the biggest * line 4: does the same, but checks if num2 is the biggest of the three * line 6: if num1 isn't the biggest and num2 isn't the biggest, then num3 must be Notes: However you write the answer, you need to make sure that your code would be correct no matter the order of your arguments. Make sure your code returns the correct value given the 6+ different combinations of arguments. 7. Write a function, consecutive, that takes two numbers as parameters and returns a Boolean if the two numbers are consecutive integers or not. ```python= def consecutive(num1, num2): if num1 - 1 == num2 or num1 + 1 == num2: return True return False ``` Notes: If a two numbers, x and y, are consecutive, that means that they are one "step" away from each other. Thus (mathematically): $x\pm 1 = y$ or $y\pm 1 = x$