Python-function, python Function Definition
Functions are organized and reusable, and used to implement a single, or associated function code segment.
Functions can improve the app's vigilance and reuse of code. You already know that Python provides many built-in functions, such as print (). But you can also create a function by yourself, which is called a user-defined function.
Syntax
def functionname( parameters ): function_suite return [expression]
Instance
def printme( string ): print string return
Function call
#! /Usr/bin/python #-*-coding: UTF-8-*-# define function def printme (string): print (string) return # Call the printme function ("call a user-defined function! ")
Parameters
The following are the formal parameter types that can be used to call a function:
- Required Parameter
- Keyword Parameter
- Default parameters
- Variable Length Parameter
Required Parameter
Required parameters must be input to the function in the correct order. The number of calls must be the same as that of the clear call.
#!/usr/bin/python# -*- coding: UTF-8 -*- def printme( string ): print( string ) returnprintme()#Traceback (most recent call last):# File "C:\Users\tiannan\Desktop\mytest.py", line 8, in <module># printme()#TypeError: printme() missing 1 required positional argument: 'string'
Keyword Parameter
Keyword parameters are closely related to function calls. function calls use keyword parameters to determine input parameter values.
When you use keyword parameters to allow function calls, the order of parameters is inconsistent with that during declaration, because the Python interpreter can use the parameter name to match the parameter value.
#!/usr/bin/python# -*- coding: UTF-8 -*-def printinfo( name, age ): print("Name:", name) print("Age:", age) returnprintinfo( age=20, name="tiannan" )#Name: tiannan#Age: 20
Default Parameter
When a function is called, if the default parameter value is not passed in, it is considered as the default value.
#!/usr/bin/python# -*- coding: UTF-8 -*-def printinfo( name, age=100 ): print("Name:", name) print("Age:", age) returnprintinfo( age=20, name="tiannan" )printinfo( name="tiannan" )#Name: tiannan#Age: 20#Name: tiannan#Age: 100
Variable Length Parameter
You may need a function to process more parameters than when it was originally declared. These parameters are called indefinite parameters. Unlike the preceding two parameters, they are not declared.
Def printinfo (arg1, * vartuple): print ("output:") print (arg1) for var in vartuple: print (var) return; printinfo (10); printinfo (70, 60, 50); # output: #10 # output: #70 #60 #50
Anonymous Functions
Python uses lambda to create anonymous functions.
Syntax
The syntax of the lambda function only contains one statement, as follows:
lambda [arg1 [,arg2,.....argn]]:expression
Example:
sum = lambda arg1, arg2: arg1 + arg2;print(sum( 10, 20 ))print(sum( 20, 20 ))#30#40
Return Statement
def sum( arg1, arg2 ): total = arg1 + arg2 return totalresult = sum( 10, 20 )print(result)#30
Variable Scope
All variables of a program are not accessible anywhere. The access permission determines where the variable is assigned.
The scope of a variable determines which specific variable name you can access in the program. The scopes of the two most basic variables are as follows:
- Global Variables
- Local variable
Def sum (arg1, arg2): total = arg1 + arg2 # print (total) total = 0sum (10, 20) # print (total) outside the Function) print (total) #30 #0
Python built-in functions