Python数据结构基础(三)——函数(Functions)

版权声明:本文版权归作者所有,未经允许不得转载! https://blog.csdn.net/m0_37827994/article/details/86491629

一、函数

函数是一种模块化可重用代码片段的方法。

  • 函数分为两块:一个是函数的定义;一个是函数的调用
  • 函数的定义即def 后面的,其中这里的参数叫做形参
  • 函数的调用时的参数叫做实参

Practise 1:

# Create a function
def add_two(x):
    x += 2
    return x

# Use the function
score = 0
score = add_two(x=score)
print (score)

输出结果:

2

Practise 2:

# Function with multiple inputs
def join_name(first_name, last_name):
    joined_name = first_name + " " + last_name
    return joined_name

# Use the function
first_name = "Goku"
last_name = "Mohandas"
joined_name = join_name(first_name=first_name, last_name=last_name)
print (joined_name)

输出结果:

Goku Mohandas

猜你喜欢

转载自blog.csdn.net/m0_37827994/article/details/86491629