1 Concepts
In general, a small function module will be made into a function.
In general, a piece of code needs to be reused multiple times, and this code has a specific function, we will put this piece of code
The organization becomes a separate function module, which can be called a function.
2. Definition and invocation of functions
Definition: Functions cannot be defined repeatedly
def function name ():
code block for feature implementation ...
Call: Function name ()
Functions can be repeated calls must be defined first and then called
1 #printing 99 multiplication tables with functions2 defshow99 ():3i = 14 whileI <10:5j = 16 whileJ <=I:7 Print("%d *%d =%d\t"% (j,i,i*j))8J +=19i + = 1Ten Print("") OneShow99 ()
3. Parameters of the function, return value
Parameters
With parameters, no parameters
return value
There is a return value, no return value
4. Encapsulate a function to complete the sum of two numbers
Parameters: 2 Parameters
Returns the value return and
Import Random
Random.randint (1,6)
1 #returns a call to a random number function2 ImportRandom3 #call the random function with the return value4Random = Random.randint (1,6)5 6 #define a function that asks for two numbers7 defGetsum (A, b):8 #sum1 = a + b9 #return sum1Ten returnA +b OneNUM1 = Int (input ("Enter first number")) Anum2 = Int (input ("Enter a second number")) - #call a custom function, get two numbers and save in Resul -result =getsum (num1,num2) the #seeking num1 and result -RESULT1 =getsum (Num1,result) -
python--function 1