Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing Functions( ) (Part 4)

Similar presentations


Presentation on theme: "Writing Functions( ) (Part 4)"— Presentation transcript:

1 Writing Functions( ) (Part 4)
Returning Values

2 Returning Values Now that we’ve created a communication street with our functions, let’s bring it back around We can define our functions to send back information to us using the “return” keyword Remember, that the functions must then be called to a variable location!

3 Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) cube(number) # this will not output anything because the returned value does not have a location nor was it printed

4 Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) new = cube(number) print(new)

5 Return Value def cube ( num1 ): cubed = num1 ** 3 return cubed number = int(input(“Give me a number:”)) print(cube(number)) Note: you can return values to be printed right away

6 Practice Define a function that passes three arguments denoting three different test scores from the user and returns the average of the test scores Then, call the function with the given test scores of 96, 88 and 90 and print out the average

7 Practice Define a main function that asks the user for the test scores and passes them into the average function you created from the previous exercise Run this main function for two different users and compare values to see who got the higher grade Print ONLY the higher grade average and congratulate that user

8 Returning Multiple Values
As with passing arguments, we can define functions to return multiple values at a time. Again there is a warning, you must return all values into the appropriate number of variable locations Values will be returned to their respective located variables

9 Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1, num2 = powers(10) # this will cause an error because the function returns 3 values and is called into only two locations

10 Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1 = powers(10) # this will not cause an error but the three values will be returned to the variable num1 as a list

11 Return Value def powers( number ): squared = number ** 2 cubed = number ** 3 fourth = number ** 4 return squared, cubed, fourth num1, num2, num3 = powers(10)

12 IPO Notation Stands for Input, Processing, and Output notation.
Programmers use IPO to describe the type of function they need to create. When given IPO notation, you must follow the rules for the number of arguments expect (input), what it should do (processing) and the number of values returned (output)

13 IPO Notation IPO notation is usually written as a comment inside of source code Example: # Input: passes 2 arguments # Processing: adds the two numbers # Output: returns 1 value as a sum def function (a, b): return a + b

14 Practice Define a function that passes four arguments (in any order), alphabetizes them and returns all four names in alphabetical order into separate variables labeled: “first”, “second”, “third”, and “fourth” Call the function with the input function passed four times as each of the required arguments and have the names printed out in alphabetical order, and then in reverse order

15 Practice Define a function that:
Input: passes four arguments Processing: alphabetizes arguments, sorts from least to greatest Output: returns four values in order from least to greatest Call the function with the input function passed four times as each of the required arguments and have the names printed out in alphabetical order, and then in reverse order

16 Practice Now, combine the main average function and the alphabetizing function to see if we can organize which of the fours users achieved the highest grade Then, print out the scores from high to low


Download ppt "Writing Functions( ) (Part 4)"

Similar presentations


Ads by Google