similar to other languages, functions in Python are called with parentheses (()). Functions must be defined before they are called. If there is no return statement in the function, the None object is returned automatically.
Python is called by reference. This means that changes to the parameters within the function affect the original object. However, in fact, only mutable objects are affected by this, and for immutable objects, it behaves like a value call.
How to define a function
def function_name ([arguments]): " Optional Documentation String " Function_suite
The syntax for defining a function consists of the DEF keyword and the function name immediately following it, plus several parameters required by the function. The function arguments (compared to the arguments in the example above) are optional, which is why you put them in brackets. (Don't write the brackets in your code!) This statement is terminated by a colon (as in the end of the IF and while statements), followed by a code group representing the body of the function, and a short example:
def addme2me (x): ' Apply + operation to argument ' return (x + x)
This function, the work of "add me to my value". It takes an object, adds its value to itself, and then returns the and. For numeric type parameters, the result is obvious, but I want to point out here that the plus operator works almost with all data types. In other words, almost all standard data types support the + operator, whether it is a numeric addition or a sequence merge.
How to call a function
>>> addme2me (4.25) 8.5>>>>>> Addme2me (10< Span style= "color: #000000;" >) 20>>>>>> Addme2me ( python ) " pythonpython " >>>>>> addme2me ([-1, abc -1, ,-1, " ABC ]
The calling function in the Python language is the same as in other high-level languages, with the function name plus the function operator, and a pair of parentheses. The parentheses are all optional parameters. The parentheses cannot be omitted, even if a parameter is not there. Notice how the + operator works in non-numeric types.
Default parameters:
The parameters of a function can have a default value, and if provided with a default value, in the function definition, the parameter is provided in the form of an assignment statement. In fact, this is just the syntax that provides the default argument, which means that if the parameter is not supplied when the function is called, it takes the value as the default value.
>>>defFoo (debug=True): .....'determine if in debug mode with default argument'... ifDebug: ...Print 'In debug mode'... Print ' Done'...>>>foo ()inchDebug Modedone>>>foo (False) done
In the example above, the debug parameter has a default value of True. If we do not pass parameters to the function foo (), Debug automatically gets a value of True. When we call Foo () for the second time, we deliberately pass a parameter False to Foo () so that the default parameter is not used.
Classes in Python are simple to explain