1. Define a function
- Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters. parameter values and parameter names are matched in the order defined in the function declaration.
- The first line of the function statement can optionally use the document string-for storing the function description.
- The function contents begin with a colon and are indented.
- Return[expression] End Function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
1 def functionname (parameters): 2 " Function _ Document string " 3 function_suite 4 return [Expression]
2. Function calls
1 #Coding=utf-82 #!/usr/bin/python3 4 #Function definition is here5 defprintme (str):6 "print any passed-in string"7 Printstr;8 return;9 Ten #Now the can call Printme function OnePrintme"I want to invoke the user custom Function!"); APrintme"call the same function again");
3. Parameters
Passing parameters by value and by reference
All parameters (independent variables) are passed by reference in Python. If you modify the parameters in the function, the original parameters are changed in the function that called the function.
1 #Coding=utf-82 #!/usr/bin/python3 4 #Writable Function Description5 defchangeme (mylist):6 "to modify an incoming list"7Mylist.append ([1,2,3,4]);8 Print "value in function:", MyList9 returnTen One #Call the Changeme function AMyList = [10,20,30]; - changeme (mylist); - Print "values outside the function:", MyList
The Python interpreter is able to match parameter values using parameter names, and the order of named parameters is not important
1 #Coding=utf-82 #!/usr/bin/python3 4 #Writable Function Description5 defPrintinfo (name, age):6 "print any passed-in string"7 Print "Name:", name;8 Print " Age", age;9 return;Ten One #Call the Printinfo function APrintinfo (age=50, name="Miki");
Variable length parameters, you may need a function that can handle more arguments than was originally declared. These parameters are called indeterminate length parameters. The variable name with an asterisk (*) will hold all the unnamed variable arguments, and you can select no more arguments.
1 #Coding=utf-82 #!/usr/bin/python3 4 #Writable Function Description5 defPrintinfo (arg1, *vartuple):6 "Print any parameters passed in"7 Print "Output:"8 Printarg19 forVarinchvartuple:Ten Printvar One return; A - #Call the Printinfo function -Printinfo (10 ); thePrintinfo (70, 60, 50 ); - -> - Output: +10 - Output: +70 A60 at50
anonymous function, the lambda function can only write one line,
- Anonymous functions cannot call print directly, because lambda requires an expression.
- A lambda function can receive any number of arguments but only return the value of an expression
1 #Coding=utf-82 #!/usr/bin/python3 4 #Writable Function Description5sum =LambdaArg1, Arg2:arg1 +arg2;6 7 #Call the SUM function8 Print "Value of total:", SUM (10, 20 )9 Print "Value of total:", SUM (20, 20)
A variable defined inside a function has a local scope, which defines a global scope outside the function
1 #Coding=utf-82 #!/usr/bin/python3 4Total = 0;#This is global variable.5 #Writable Function Description6 defsum (arg1, arg2):7 #returns the and of the 2 parameters.8Total = Arg1 + arg2;#Total Here is the local variable.9 Print "Inside The function local total:", TotalTen returnTotal ; One A #Call the SUM function -SUM (10, 20 ); - Print "Outside The function global total:", total
>30,30
Python Learning note seven: functions