function definition
The function code block begins with a def keyword followed by the function identifier name and parentheses ().
Any incoming parameters and arguments must be placed in the middle of the parentheses. Parentheses can be used to define parameters.
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] ends the function, optionally returning a value to the caller. Return without an expression is equivalent to returning None.
def functionname (parameters):
"Function _ Document String"
Function_suite
return [expression]
Function call: Direct write function name (parameter)
def printtest (str):
Print (str)
Return
Printtest (' AA ')
Can change (mutable) and non-changing (immutable) objects
Can change: List and dictionary
Non-changing: strings, tuples, and numbers are immutable objects
Everything in Python is an object, strictly meaning we can't say value passing or reference passing, we should say immutable objects and pass mutable objects
Passing immutable object instances
#!/usr/bin/python
Def changeint (a):
A = 10
b = 2
Changeint (b)
Print B # result is 2
Passing a Mutable object instance
#!/usr/bin/python
def changeme (mylist):
"Modify Incoming List"
Mylist.append ([1,2,3,4]);
Print "function value:", MyList
Return
MyList = [10,20,30];
Changeme (MyList);
Print "function outside value:", MyList
Output results
Values in function: [10, 20, 30, [1, 2, 3, 4]]
Out-of-function values: [10, 20, 30, [1, 2, 3, 4]]
Parameters:
Prerequisites: Required parameters must be passed into the function in the correct order. The number of calls must be the same as when declared.
Call the Printme () function, you must pass in a parameter, or a syntax error will occur
Keyword parameters
#!/usr/bin/python
#可写函数说明
def printme (str):
"Print any incoming string"
Print str;
Return
#调用printme函数
Printme (str = "My string");
Default parameters
When a function is called, the value of the default parameter is considered the default value if it is not passed in
#!/usr/bin/python
#可写函数说明
def printinfo (name, age = 35):
"Print any incoming string"
Print "Name:", name;
Print "Age", age;
Return
#调用printinfo函数
Printinfo (age=50, name= "Miki");
Printinfo (name= "Miki");
Indefinite length parameter
You may need a function that can handle more arguments than was originally declared. These parameters are called variable length parameters, and are not named when declared, unlike the above 2 parameters
#!/usr/bin/python
def printinfo (Arg1, *vartuple):
"Print any parameters passed in"
Print "Output:"
Print Arg1
For Var in vartuple:
Print Var
Return
Printinfo (10);
Printinfo (70, 60, 50);
Return statement
Return statement [expression] exits the function, optionally returning an expression to the caller. Return statement with no parameter value returns none
def sum (arg1, arg2):
Total = Arg1 + arg2
Print "in function:", total
return total;
Total = SUM (10, 20);
Python notes-functions