Python -- function, python Function Definition
Function
I. Introduction
Definition: A function encapsulates a set of statements by a name (function name). to execute this function, you only need to call its function name.
Features:
Syntax definition
Def func (): # function name print ("Hello, world") func () # Call a function
With Parameters
Def add (a, B): c = a + breturn c # Return the function execution result f = add (1, 2)
Ii. Function Parameters
2.1 Form parameters and real parameters
The Parameter Variable allocates memory units only when called. The allocated memory units are immediately released at the end of the call. Therefore, the parameter is valid only within the function. After the function call ends, you cannot use this variable after the main function is called.
Def add (a, B): # a, B is the form parameter
Real parameters can be constants, variables, expressions, functions, etc. No matter what type of real parameters are, they must have a definite value during function calling, in order to pass these values to the form parameter. Therefore, values should be pre-assigned, input, and other methods to obtain a definite value for the parameter.
F = add (1, 2) #1, 2 real parameters
2.2 default parameters