A function is a well-organized, reusable piece of code that is used to implement a single, or associated function.
functions can improve the modularity of the application and the reuse of code. You already know that Python provides a lot of built-in functions, such as print(). But you can also create functions yourself, which is called a user-defined function.
Define a function
You can define a function that you want to function. Here are the simple rules:
- 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 between the parentheses. Parentheses can be used to define parameters.
- The first line of the function can optionally use a docstring—for storing function descriptions.
- The function content starts with a colon and is indented.
- return [expression] Ends the function, optionally returning a value to the caller. A return without an expression is equivalent to returning None.
By default, parameter values and parameter names are matched in the order defined in the function declaration.
The following is a simple Python function that takes a string as an incoming parameter and prints it to a standard display device.
Instance (Python 2.0+)
Defining a function only gives the function a name, specifies the parameters contained in the function, and the structure of the code block.
After the basic structure of this function is complete, you can execute it through another function call, or you can execute it directly from the Python prompt.
The following example calls the printme() function:
Instance (Python 2.0+)
The output of the above example:
In Python, types are objects, and variables are of no type:
In the above code, [1,2,3] is a List type, "WELOOKUPS" is a String type, and the variable a is of no type, she is just a A reference to an object (a pointer), either a List type object or a String type object.
mutable and immutable objects
In Python, strings, tuples, and numbers are unchangeable objects, while list, dict, etc. are objects that can be modified.
Immutable type: variable assignment a=5 and then assign a=10, here is actually a new int value object 10, then let a point to it , and 5 is discarded, not changing the value of a, which is equivalent to newly generating a.
Variable type: variable assignment la=[1,2,3,4] and then assign la=5 is the list la The third element value changes, itself la does not move, only a part of its internal value is modified.
Parameter passing of python functions:
Immutable types: are similar to c++ value passing, such as integers, strings, tuples. For fun(a), only the value of a is passed, which does not affect the a object itself. For example, modifying the value of a inside fun(a) simply changes another copied object without affecting a itself.
Variable types: are similar to c++'s reference passing, such as lists, dictionaries. For example, fun(la), the la is actually passed, and the la outside the modified fun will also be affected
Everything in Python is an object. Strictly speaking, we can't say value passing or reference passing. We should say passing immutable objects and passing variable objects.