函数是指代码片断,能够重复调用,好比咱们前面文章接触到的type()/len()等等都是函数,这些函数是python的内置函数,python底层封装后用于实现某些功能。python
在Python中,定义一个函数要使用def
语句,依次写出函数名、括号、括号中的参数和冒号:,而后,在缩进块中编写函数体,函数的返回值用return语句返回;若是没有return语句,默认返回None:git
1github 2express 3微信 4ide |
def functionname( parameters ):函数 "函数说明"字体 function_suiteui return [expression]spa |
例如:写一个函数输出’hello world’
1 2 |
def cusom_print(): print("hello world") |
当在py文件中,代码一行一行执行,若是遇到函数的定义,编译器会自动跳过,执行函数以后的代码,若是想调用函数直接调用便可。
注意:函数在调用以前必须先声明。python中的内置函数如:print/type函数等等已经在python编译器内部声明而且定义好了,咱们只管调用便可,不须要关心具体内部如何实现。示例代码以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# !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() |
输出结果:
1 2 3 |
hello world hello world hello world |
代码分析:代码执行到第15行时,编译器发现这是一个函数声明,编译器并不会执行,会自动跳到函数末尾第20行,编译器发现20行是在调用custom_print()函数,会直接进入custom_print()函数执行函数内的代码第16/17/18行直到函数结束,这就是整个运行过程。
函数能够经过外部传递参数,好比:print()函数,能够直接传递字符串并打印字符串;也能够不传递参数,好比上面的custom_print函数,根据本身的需求而定.
函数声明的时候定义的参数叫作形参;外部调用函数传递的参数叫作实参;函数的参数有二者类型:
常规而言,函数默认有几个形参,在外部调用时就须要传递多少个实参,示例代码以下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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) |
输出结果:
1 2 3 4 5 6 |
cusom_print1 : x=1 cusom_print2 : x=1 cusom_print2 : y=2 cusom_print3 : x=1 cusom_print3 : y=2 cusom_print3 : z=3 |
在函数参数中,除了常规参数还有缺省参数,即缺省参数有一个默认值,若是外部调用该函数没有给缺省参数传递参数,该形参直接取默认参数值;若是外部调用时给缺省参数传递了参数,那么该形参的值应该等于外部传递的参数,带有缺省参数的函数也被称为缺省函数,示例代码以下:
1 2 3 4 5 6 7 8 9 |
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) |
输出结果:
1 2 3 4 5 6 7 8 9 10 11 12 |
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.缺省参数必须写在函数形参的末尾
1 2 3 |
# 错误写法 def cusom_print4(x,y=2,z): print("cusom_print4 : x={}".format(x)) |
除了上面二者,在函数的参数中还有一种不定长参数,即:函数的形参长度/类型都不固定,可能听着有点蒙,这个问题咱们留到下一篇文章 python 函数不定长参数 *argc,**kargcs 讲解,暂时不作过多解释。
函数的返回值无关紧要,根据本身的使用需求而定。若是函数没有return返回值,默认会返回None,即空值。和 False 不一样,它不表示 0,也不表示空字符串,而表示没有值,也就是空值。
1.函数的声明必须在调用以前,不然会报错.
2.注意缺省参数的参数写法
3.函数没有使用return,默认返回None
4.python 函数不定长参数 *argc,**kargcs
转载请注明:猿说Python » python函数声明和调用