Day 10 函数

day10思维导图

一 函数 function

A function is a block of code which only runs when it is called.

You can pass data, known as parameters, into a function.

A function can return data as a result.

Advantage 函数的作用

Reducing duplication of code.

Decomposing complex problems into simpler pieces.

Improving clarity of the code.

Reuse of code.

Information hiding.

Define a function 定义函数

You can define functions to provide the required functionality. Here are simple rules to define a function in Python.

Function blocks begin with the keyword def followed by the function name and parentheses ( ( ) ).

Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.

The first statement of a function can be an optional statement - the documentation string of the function or docstring.

The code block within every function starts with a colon (:) and is indented.

The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Arguments 参数

Information can be passed into functions as arguments.

Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.

 

Calling a Function 调用函数

Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.

Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt.

Examples:

# 使用无参函数
def say():
  print("1")
  print("2")
  print(345)

say()
say()
# 调用有参函数的返回值
a = int(input("数字1"))
b = int(input("数字2"))

def max(a, b):
  if a >= b:
      return (a)
  else:
      return (b)

print("较大的数为:", max(a, b))

二 参数 Arguments and Parameters

Parameters or Arguments?形参还是实参?

From a function's perspective:

A parameter is the variable listed inside the parentheses in the function definition.

An argument is the value that is sent to the function when it is called.

Keyword Arguments 关键字实参

You can also send arguments with the key = value syntax.

This way the order of the arguments does not matter.

# 位置实参,关键字实参
def foo(x,y,z):
  pass

foo(1,y=2,3)   # 错误(位置参数必须在关键字参数之前)
foo(1,y=2,z=3,x=4)   # 错误(不能重复赋值)

The phrase Keyword Arguments are often shortened to kwargs in Python documentations.

Default Parameters 默认形参

def func(name,age,gender="male"):
  print(name)
  print(age)
  print(gender)
func("sam",20)
func("lily",25,"female")

可变长参数

Arbitrary Arguments, *args

接收溢出的位置实参

If you do not know how many arguments that will be passed into your function, add a * before the parameter name in the function definition.

This way the function will receive a tuple of arguments, and can access the items accordingly:

def my_function(*kids):
print("The youngest child is " + kids[2])

my_function("Emil", "Tobias", "Linus")
将可迭代类型,打散为位置实参
def func(x, y, z):
  print(x)
  print(y)
  print(z)

func(*[11, 22, 33])

 

Arbitrary Keyword Arguments, **kwargs

接收溢出的关键字实参

If you do not know how many keyword arguments that will be passed into your function, add two asterisk: ** before the parameter name in the function definition.

This way the function will receive a dictionary of arguments, and can access the items accordingly:

def my_function(**kid):
print("His last name is " + kid["lname"])

my_function(fname = "Tobias", lname = "Refsnes")
将字典代类型,打散为关键字实参
def func(x, y, z):
  print(x)
  print(y)
  print(z)


func(**{"x": 11, "y": 22, "z": 33})
原文地址:https://www.cnblogs.com/fengshili666/p/14201085.html