目录
- 一.前言
- 二.Python 函数定义
- 三.Python 函数的调用
-
四.Python 函数传参
- 1.Python 函数常规参数
- 2.Python 函数缺省参数
- 3.Python 函数不定长参数
- 五.Python 函数返回值 return
- 六.Python 函数重点总结
- 七.猜你喜欢
零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门
一.前言
Python 函数是指代码片段,可以重复调用 ,比如我们前面文章接触到的 type / id 等等都是函数,这些函数是 Python 内置函数,Python 底层封装后用于实现某些功能 ,对于所有的 Python 内置函数,我们只管调用即可,不需要关心具体内部如何实现。
二.Python 函数定义
在 Python 中 ,定义一个函数要使用
**def**语句,依次写出函数名、括号、括号中的参数和冒号:,然后在缩进块中编写函数体 ,函数的返回值用 return 语句返回;如果没有 return 语句,默认返回 None ;
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python 函数使用.py @Time:2021/3/29 08:00 @Motto:不积跬步无以至千里,不积小流无以成江海 ,程序人生的精彩需要坚持不懈地积累! """ def functionname( parameters ): "函数说明" function_suite return [expression] ''' 例如:定义一个函数输出’hello world’ ''' def cusom_print(): print("hello world")
三.Python 函数的调用
当在 py 文件中 ,代码一行一行执行,如果遇到函数的定义,编译器会自动跳过 ,执行函数之后的代码,如果想调用函数直接调用即可 。
注意:函数在调用之前必须先声明。Python 中的内置函数如:print / type 函数等等已经在 Python 编译器内部声明并且定义好了,我们只管调用即可 ,不需要关心具体内部如何实现。示例代码如下:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python 函数使用.py @Time:2021/3/29 08:00 @Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ def custom_print(): print("hello world") print("hello world") print("hello world") custom_print() ''' 输出结果: hello world hello world hello world '''
代码分析:代码执行到第 15 行时 ,编译器发现这是一个函数声明,编译器并不会执行,会自动跳到函数末尾第 20 行 ,编译器发现 20 行是在调用 custom_print 函数,会直接进入 custom_print 函数执行函数内的代码第 16 / 17 / 18 行直到函数结束,这就是整个运行过程 。
四.Python 函数传参
函数可以通过外部传递参数 ,参数分为形参和实参:
def cusom_print(x): //x 是函数的形参 - 用于函数声明 print("cusom_print : x={}".format(x)) cusom_print(1) //1 是函数的实参 - 用于调用函数时参数传递
1.Python 函数常规参数
常规而言 ,函数默认有几个形参,在外部调用时就需要传递多少个实参,示例代码如下:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python 函数使用.py @Time:2021/3/29 08:00 @Motto:不积跬步无以至千里 ,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累! """ def cusom_print1(x): print("cusom_print1 : x={}".format(x)) def cusom_print2(x,y): print("cusom_print2 : x={}".format(x)) print("cusom_print2 : y={}".format(y)) def cusom_print3(x,y,z): print("cusom_print3 : x={}".format(x)) print("cusom_print3 : y={}".format(y)) print("cusom_print3 : z={}".format(z)) cusom_print1(1) cusom_print2(1,2) cusom_print3(1,2,3) ''' 输出结果: cusom_print1 : x=1 cusom_print2 : x=1 cusom_print2 : y=2 cusom_print3 : x=1 cusom_print3 : y=2 cusom_print3 : z=3 '''
2.Python 函数缺省参数
在 Python 函数参数中,除了常规参数还有缺省参数 ,即缺省参数有一个默认值,
- 如果外部调用该函数没有给缺省参数传递参数,该形参直接取默认参数值;
- ** 如果外部调用时给缺省参数传递了参数 ,那么该形参的值应该等于外部传递的参数;**
带有缺省参数的函数也被称为缺省函数,示例代码如下:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:猿说编程 @Blog(个人博客地址): www.codersrc.com @File:Python 函数使用.py @Time:2021/3/29 08:00 @Motto:不积跬步无以至千里,不积小流无以成江海 ,程序人生的精彩需要坚持不懈地积累! """ def cusom_print4(x,y=2,z=3): # x=2,z=3 缺省参数 print("cusom_print4 : x={}".format(x)) print("cusom_print4 : y={}".format(y)) print("cusom_print4 : z={}".format(z)) print("***"*20) cusom_print4(1) cusom_print4(1,4) cusom_print4(1,4,3) ''' 输出结果: cusom_print4 : x=1 cusom_print4 : y=2 cusom_print4 : z=3 ************************************************************ cusom_print4 : x=1 cusom_print4 : y=4 cusom_print4 : z=3 ************************************************************ cusom_print4 : x=1 cusom_print4 : y=4 cusom_print4 : z=3 ************************************************************ '''
注意:
-
1.缺省参数都有一个默认值,如果外部没有给缺省参数传递参数,那么直接取默认值;否则等于外部传递的参数值;
-
2.缺省参数必须写在函数形参的末尾;
错误写法
def cusom_print4(x,y=2,z):
print(“cusom_print4 : x={}”.format(x))
3.Python 函数不定长参数
除了上面两者 ,在函数的参数中还有一种不定长参数 ,即:函数的形参长度/类型都不固定,可能听着有点蒙,这个问题我们留到下一篇文章 Python 函数不定长参数 *argc , **kargcs 讲解 ,暂时不做过多解释。
五.Python 函数返回值 return
函数的返回值可有可无,根据自己的使用需求而定。如果函数没有 return 返回值,默认会返回 None(即空值) ,与 False 不同,它不表示 0,也不表示空字符串 ,而表示没有值,也就是空值 。
六.Python 函数重点总结
- 1.函数的声明必须在调用之前,否则会报错;
- 2.注意缺省参数的参数写法;
- 3.函数没有使用 return ,默认返回 None ;
七.猜你喜欢
- Python 配置环境
- Python 变量
- Python 运算符
- Python 条件判断 if/else
- Python while 循环
- Python break
- Python continue
- Python for 循环
- Python 字符串
- Python 列表 list
- Python 元组 tuple
- Python 字典 dict
- Python 条件推导式
- Python 列表推导式
- Python 字典推导式
未经允许不得转载:猿说编程 » Python 函数声明和调用
本文由博客 - 猿说编程 猿说编程 发布!