Python Functions

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

The value between the parentheses when we call the function (in this case, "peanut butter") is referred to as an __________. The (1) is the information that is to be used in the _______ of the function. When we then call the function, Python assigns the ______ parameter name special_item with the ______ parameter data, "peanut_butter". In other words, it is as if this line was included at the top of the function: Every time we call greet_customer() with a different value between the parentheses, special_item is ____________.

argument of the function call execution formal actual assigned to hold that value

functions are ______ to use a function, you have to _____

collections of code call it

We can also define ______ arguments for a function using syntax very similar to our keyword-argument syntax, but used ________. If the function is called without an argument for that parameter, _________. Once you give an argument a (1) value (making it a keyword argument), no arguments that follow can be used positionally. For example:

default during the function definition it relies on the default def greet_customer(special_item,grocery_store="Engrossing Grocers"): print("Welcome to "+ grocery_store + ".") print("Our special is " + special_item + ".") print("Have fun shopping!")

A return statement is used to _________ and _________ to the caller. The statements after the return statements are not executed. If the return statement is without any expression, then the special value None is returned. Note: Return statement can not be used _____ the function.

end the execution of the function call and "returns" the result (value of the expression following the return keyword) outside

We can also pass these arguments as __________, where we explicitly refer to what each argument is assigned to in the function call.

keyword arguments greet_customer(special_item="chips and salsa", grocery_store="Stu's Staples")

A lot of times when we're calling a python function We call it And it just does it's thing And we move on with the program ___________ But sometimes when we call a function we're actually going to want to get information back from that function _________ The function will be like "hey, here's some information" And that's what the______ key does

(Collection or code) (Mapping) return

In order to call a function, we use the syntax__________. The parentheses are important! They make the code inside the function run. In this example, the function call looks like:_________

1 function_name()

A function is a _____________. By calling a function, we can ____________, without having to repeat ourselves. So, a function is a tool that you can use over and over again to produce __________.

1 collection of several lines of code 2 call all of these lines of code at once consistent type of outputs from different inputs

The keyword def tells Python that we are ________. This function is called greet_customer. Everything that is indented after the : is what _________. So every time we call greet_customer(), the three print statements run.

1 defining a function 2 is run when greet_customer() is called

To write a function, you must have a ______ and an ________. The (1) starts with the keyword _____ and the name of the function, followed by ________, and a ______. The indented block of code performs some sort of operation. This syntax looks like:

1 heading 2 indented block of code 3 def 4 parentheses 5 colon 6 def function_name(): some code

Also

Let's say we have our function from the last exercise that creates a string about a special item: def create_special_string(special_item): return "Our special is " + special_item + "." What if we wanted to access the variable special_item outside of the function? Could we use it? def create_special_string(special_item): return "Our special is " + special_item + "." print("I don't like " + special_item) If we try to run this code, we will get a NameError, telling us that 'special_item' is not defined. The variable special_item has only been defined inside the space of a function, so it does not exist outside the function. We call the part of a program where special_item can be accessed its scope. The scope of special_item is only the create_special_string function. Variables defined outside the scope of a function may be accessible inside the body of the function: header_string = "Our special is " def create_special_string(special_item): return header_string + special_item + "." print(create_special_string("grapes")) There is no error here. header_string can be used inside the create_special_string function because the scope of header_string is the whole file. This file would produce: Our special is grapes.

So far, we have only seen functions that print out some result to the console. Functions can also return a value to the user so that this value can be _______. When there is a result from a function that can be stored in a _____, it is called a _______. We use the keyword ______ to do this.

modified or used later returned function value variable return def divide_by_four(input_number): return input_number/4 result = divide_by_four(16) # result now holds 4 print("16 divided by 4 is " + str(result) + "!") result2 = divide_by_four(result) print(str(result) + " divided by 4 is " + str(result2) + "!") 16 divided by 4 is 4! 4 divided by 4 is 1! def create_special_string(special_item): return "Our special is " + special_item + "." special_string = create_special_string("banana yogurt") print(special_string) Our special is banana yogurt.

Let's return to Engrossing Grocers. The special of the day will not always be mandarin oranges, it will change every day. What if we wanted to call these three print statements again, except with a variable special? We can use ________, which are variables that you can pass into the function when you call it.

parameters def greet_customer(special_item): print("Welcome to Engrossing Grocers.") print("Our special is " + special_item + ".") print("Have fun shopping!") In the definition heading for greet_customer(), the special_item is referred to as a formal parameter. This variable name is a placeholder for the name of the item that is the grocery's special today. Now, when we call greet_customer, we have to provide a special_item:

Whichever value is put into greet_customer() first is assigned to grocery_store, and whichever value is put in second is assigned to special_item. These are called ________ because their assignments depend on their positions in the_______l. Using two parameters or more in a function is also called having _____-

positional arguments function call multiple parameters def greet_customer(grocery_store, special_item): print("Welcome to "+ grocery_store + ".") print("Our special is " + special_item + ".") print("Have fun shopping!")

Variables defined outside the _______ may be accessible inside the body of the function:

scope of a function header_string = "Our special is " def create_special_string(special_item): return header_string + special_item + "." print(create_special_string("grapes"))

Sometimes we may want to return more than one value from a function. We can return several values by _______: This function takes in an x value and a y value, and returns them both, squared. We can get those values by assigning them both to variables when we call the function:

separating them with a comma def square_point(x_value, y_value): x_2 = x_value * x_value y_2 = y_value * y_value return x_2, y_2 x_squared, y_squared = square_point(1, 3) print(x_squared) print(y_squared) 1 9

In Python, the amount of ________ tells the computer what is part of a function and what is not part of that function. If we wanted to write another line outside of greet_customer(), we would have to _______

whitespace unindent the new line


संबंधित स्टडी सेट्स

hyperventilation vs. hypoventilation

View Set

Managerial Accounting Exam 2 Practice Exam

View Set

High-Risk Postpartum- Davis Practice Q's

View Set

macroeconomics chapter 16 - inflation and unemployment in the long run

View Set