Do you want to understand the basics of computer functions and lay a solid foundation for your programming journey? PHP editor Apple brings you a comprehensive guide, providing clear and easy-to-understand explanations, covering basic concepts, function types and usage scenarios. Come explore this article, master the essence of computer functions, and unlock new dimensions of programming skills!
1. Function definition, function name, function body, and definition syntax for calling the
function:
def function name ( ):
Function body
Call of function: Use the function name to call the function, written as: function name(), at this time the function body will be executed
2. Function Return
After executing the function, you can use return to return the result to the function name ().
The use of return in functions:
1) When the function encounters return, the function ends and execution will no longer continue.
2) Give the function caller (function name + ()) an access result
3. Parameters of the function
Parameters, the function specifies a specific one when calling The value of the variable.
def Function name (parameter list):
Function body
Function name (parameter)
1)Formal parameter
The variables written at the position where the function is declared are called formal parameters. They are complete in form but the actual parameters must be given when calling.
2) Actual parameters
are passed to the function when the function is called. The value is called the actual parameter
3) Parameter passing
The process of passing the actual parameters to the formal parameters when passing information to the function is called parameter passing.
4-1) Positional parameters
means that when passing parameters to a function, values are passed in order
4-2)Default parameters
Definition: When writing a function, you directly pass the default value to the parameter. When the function is called, the default parameter already has a value, so there is no need to pass a value.
Function: The biggest benefit is to reduce the difficulty of calling functions.
def power(m, n=3):
result=1
while n>0:
n=n-1
result=result*m
return result
# Call the function and output the result
print(power(4))
Set default parameters There are two points to note:
First: the required parameters come first and the default parameters come last, otherwise the python interpreter will report an error.
Second: The default parameters must point to immutable objects! Point to immutable object! Point to immutable object!
(Note: Strings, numbers, and tuples in python can all be regarded as objects.)
Why should we design immutable objects like str and None? Because once an immutable object is created, the data inside the object cannot be modified, which reduces errors caused by modifying data. In addition, since the object does not change, there is no need to lock the object when reading it simultaneously in a multi-tasking environment, and there is no problem at all when reading it at the same time. When we write a program, if we can design an immutable object, then try to design it as an immutable object
4-3) Keyword parameter
Definition: Variable parameters allow you to pass in 0 or any number of parameters. These variable parameters are automatically assembled into a tuple when the function is called.
The keyword parameters allow you to pass in 0 or any number of parameters containing parameter names. These keyword parameters are automatically assembled into a dict inside the function. When calling a function, you can only pass in the required parameters:
Function: Extend the functionality of the function
Features: **kw
Classification of parameters:
From the perspective of actual parameters:
1. Positional parameters assign values to formal parameters according to their positions
2. Keyword parameters pass parameters to functions according to the naming of formal parameters
3. Mixed use: write positional parameters first, then write keyword parameters
Standing in the formal parameter supervision:
1. Positional parameters
2. If the default value parameter is given Just get a value. If you don’t give a value, use the default value
The above is the detailed content of Basic knowledge of computer functions?. For more information, please follow other related articles on the PHP Chinese website!