python函数声明和调用(18)

2022-12-26,,,

函数是指代码片段,可以重复调用,比如我们前面文章接触到的type()/len()等等都是函数,这些函数是python的内置函数,python底层封装后用于实现某些功能。

一.函数的定义

在Python中,定义一个函数要使用def语句,依次写出函数名、括号、括号中的参数和冒号:,然后,在缩进块中编写函数体,函数的返回值用return语句返回;如果没有return语句,默认返回None:

def functionname( parameters ):
"函数说明"
function_suite
return [expression]

例如:写一个函数输出’hello world’

def cusom_print():
print("hello world")

二.函数的调用

当在py文件中,代码一行一行执行,如果遇到函数的定义,编译器会自动跳过,执行函数之后的代码,如果想调用函数直接调用即可。

注意:函数在调用之前必须先声明。python中的内置函数如:print/type函数等等已经在python编译器内部声明并且定义好了,我们只管调用即可,不需要关心具体内部如何实现。示例代码如下:

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:何以解忧
@Blog(个人博客地址): shuopython.com
@WeChat Official Account(微信公众号):猿说python
@Github:www.github.com @File:python_function.py
@Time:2019/10/3 10:48 @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行直到函数结束,这就是整个运行过程。

三.函数传参

函数可以通过外部传递参数,比如:print()函数,可以直接传递字符串并打印字符串;也可以不传递参数,比如上面的custom_print函数,根据自己的需求而定.

函数声明的时候定义的参数叫做形参;外部调用函数传递的参数叫做实参;函数的参数有两者类型:

1.常规参数

常规而言,函数默认有几个形参,在外部调用时就需要传递多少个实参,示例代码如下:

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.缺省参数

在函数参数中,除了常规参数还有缺省参数,即缺省参数有一个默认值,如果外部调用该函数没有给缺省参数传递参数,该形参直接取默认参数值;如果外部调用时给缺省参数传递了参数,那么该形参的值应该等于外部传递的参数,带有缺省参数的函数也被称为缺省函数,示例代码如下:

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 函数不定长参数 *argc,**kargcs 讲解,暂时不做过多解释。

四.函数返回值return

函数的返回值可有可无,根据自己的使用需求而定。如果函数没有return返回值,默认会返回None,即空值。和 False 不同,它不表示 0,也不表示空字符串,而表示没有值,也就是空值。

五.重点总结

1.函数的声明必须在调用之前,否则会报错.

2.注意缺省参数的参数写法

3.函数没有使用return,默认返回None

猜你喜欢:

1.pycharm配置开发模板/设置字体大小

2.python列表推导式

3.python字典推导式

4.python 函数不定长参数 *argc,**kargcs

转载请注明:猿说Python » python函数声明和调用

技术交流、商务合作请直接联系博主
扫码或搜索:猿说python

猿说python
微信公众号 扫一扫关注

python函数声明和调用(18)的相关教程结束。

《python函数声明和调用(18).doc》

下载本文的Word格式文档,以方便收藏与打印。