Notes 6 --- Python function

Chapter VIII function

 8.1 Defined Functions

Use the keyword def to a function. This is the function definition.

 

greet_user DEF (): 
Print ( "the Hello!")
greet_user ()

Code Output:
! the Hello

8.1.1 information transfer function

Username may be added in the function definition def greet_user () parentheses. Pass through here to add username, you can specify that a function accepts any value to the username you specify. Now, this function requires you call it when you specify a value for the username. When you call greet_user (), a name can be passed to it.

greet_user DEF (username): 
Print ( "the Hello," username.title + () + "!")
greet_user ( 'Jesse')

: code output
the Hello, Jesse!
8.1.2 arguments and parameters
defining function greet_user () , username required to specify a value for the variable. When you call this function and provide this information (names), it prints the appropriate greeting.
Function definition greet_user (), the username is a variable parameter - a function of the information needed to complete their work.
In the code greet_user ( 'jesse'), the value of 'jesse' is an argument.
Argument is passed to the function information of the function is called.
When we call the function, the information will make use of the function in parentheses.
In greet_user ( 'jesse'), the argument is 'jesse' to the transfer function greet_user (), this value is stored in the username parameter.
8.2 pass arguments

  The function call may also contain multiple arguments. In many ways to the transfer function arguments, argument using the position, which requires the same order as the order of arguments formal parameters; Keyword argument can also be used.
 

  8.2.1 Location argument

  The simplest way of association is based on the order of arguments. This position is referred to as an associated manner argument.

def describe_pet(animal_type, pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster', 'harry')

代码输出:

   I have a hamster.
   My hamster's name is Harry.

  1) call the function multiple times

  You can call the function as many times as needed.

def describe_pet(animal_type, pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('hamster', 'harry')
describe_pet('dog', 'willie')

Code output:

     I have a hamster.
   My hamster's name is Harry.

   I have a dog.
   My dog's name is Willie.

 2) argument ordinal position is important

 使用位置实参来调用函数时,如果实参的顺序不正确,结果可能出乎意料。

def describe_pet(animal_type, pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet('harry', 'hamster')

代码输出:

    I have a harry.
    My harry's name is Hamster.

 8.2.2 关键字实参

 关键字实参 是传递给函数的名称—值对。

def describe_pet(animal_type, pet_name):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(animal_type='hamster', pet_name='harry')

代码输出:

   I have a hamster.
   My hamster's name is Harry.

注:关键字实参的顺序无关紧要,因为Python知道各个值该存储到哪个形参中。

      describe_pet(animal_type='hamster', pet_name='harry')
   describe_pet(pet_name='harry', animal_type='hamster')

8.2.3 默认值
编写函数时,可给每个形参指定默认值
在调用函数中给形参提供了实参时,Python将使用指定的实参值;否则,将使用形参的默认值。因此,给形参指定默认值后,可在函数

调用中省略相应的实参。使用默认值可简化函数调用,还可清楚地指出函数的典型用法。

def describe_pet(pet_name, animal_type='dog'):
print("\nI have a " + animal_type + ".")
print("My " + animal_type + "'s name is " + pet_name.title() + ".")
describe_pet(pet_name='willie')

代码输出:

   I have a dog.
  My dog's name is Willie.

8.2.4 等效的函数调用

 

 

 

 
 
 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wushuai514/p/11164965.html