0% found this document useful (0 votes)
8 views1 page

CH 12 Lesson 2

The document discusses the use of input parameters in functions, explaining how to define them, assign default values, and use named parameters. It also covers the return statement, detailing how many can be used and the behavior when one is executed. Additionally, it outlines options for handling returned data from functions, including ignoring it, storing it in a variable, or using it immediately.

Uploaded by

murasakibaraxme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views1 page

CH 12 Lesson 2

The document discusses the use of input parameters in functions, explaining how to define them, assign default values, and use named parameters. It also covers the return statement, detailing how many can be used and the behavior when one is executed. Additionally, it outlines options for handling returned data from functions, including ignoring it, storing it in a variable, or using it immediately.

Uploaded by

murasakibaraxme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Ch 12 lesson 2

What are some example functions you have used that required input parameters?
str.format() function to produce a string and the random.randrange() function to generate an integer
How do you de ne and require input parameters in your own function?
Should be identi ed by names that are listed inside the parentheses in the function "def" statement.
These names are called parameters. Ex. def greet (name, town): name and town and parameters 1
and 2.
What happens in you accidentally call a function with positional parameters in the wrong order?
Its up to us as the programmers to ensure that you pass in the required parameters in the correct
order, as de ned by the function.
How do you assign default values to your function parameters?
You can use a default parameter to assign a default value to a parameter in your function de nition.
Default parameter: Function parameters that have a default value that will be used if no input data is
provided.
To de ne default values for parameters in your own function, simply enhance the "def" statement to
set parameters equal to some default value. The example below assigns the default value "Atlanta" to
the town variable in the greet() function.

def greet(name, town = "Atlanta"):


How can function callers use named parameters to ensure input values are assigned to speci c
parameters?
To use named parameters, you don't need to do anything special in your function "def" statement.
Python automatically allows anyone calling a function to use named parameters. In the example
below, all three function calls to greet() will do the same thing. In cases where we pass in named
"town" and "name" parameters, the order doesn't matter because each is speci cally assigned to a
parameter by name.

greet(town="Philadelphia", name="Betsy Ross") # use named parameters in any order


greet(name="Betsy Ross", town="Philadelphia") # use named parameters in any order
greet("Betsy Ross","Philadelphia") # rely on positional order
What keyword is used to return data from functions?
"return"
How many times can you add a return statement to a function? What happens when the rst return is
executed?
You can use more than one return statement. As your logic ows through the function, the rst time it
hits any return statement, the function will end and any output data is sent back immediately to the
calling code. The remaining lines in the function body are skipped.
What options does the calling code have to handle data returned by a function?
1. Ignore the value completely, and it will be discarded
2. Store the value in a variable for later use
3. Use the value immediately in some other expression or statement

The three examples below demonstrate each of these approaches.

calculate_grade(85) # ignore the return data


grade = calculate_grade(85) # store the return data for later use
print("Your letter grade is: ",calculate_grade(85)) # use the return data immediately

You might also like