[Python] 06_Python basic function of

1. Definition Function

def function name ():

  Package function code

  ……

Note: before and after the function should be reserved two blank lines

2. Use function

  Direct use of the function name () calls the function block.

1 def say_hello():
2     print("Hello")
3     
4 
5 say_hello()

Note: Calling functions can not appear before the defined functions.

Because the line is interpreted languages, the interpreter does not know the definition of this function.

NameError: name 'say_hello' is not defined

3. function parameters

Fill in the parameters inside the parentheses after the function name

def function name (parameter 1, parameter 2, parameter 3, ......):

  Package function code

  ……

. 1  DEF sum_2_num (num1, num2):
 2      "" " two summed number " "" 
. 3      Print (+ num1 num2)
 . 4  
. 5  
. 6 sum_2_num (333, 555)

 

4. and parameter arguments

  • Parameter : the definition of the function is, in parentheses parameter is used with no parameters, within the function as a variable
  • Arguments : calling a function, parameter parentheses, it is used to transfer data to the internal functions with

NOTE: The above code: num1, num2 is a parameter; is argument 333,555

The return value of the function

Use the return keyword to return results in the function

. 1  DEF sum_2_num (num1, num2):
 2      "" " two summed number " "" 
. 3      return num1 + num2
 . 4  
. 5  
. 6 SUM1 = sum_2_num (333, 555 )
 . 7  Print (SUM1)

 

Note: return as a function of the end, after the code is invalid.

 1 def print_line(char, times):
 2 
 3     print(char * times)
 4 
 5 
 6 def print_lines(char, times, rows):
 7     row = 0
 8     while row < rows:
 9         print_line(char, times)
10         row += 1
11 
12 
13 print_lines("*", 20, 5)

 

Achieve definable number of rows to print characters, the number of printed characters function, the console output:

********************
********************
********************
********************
********************

It 6.PyCharm adds comments to the document

1. Note the use of three functions under the function definition quotes

In PyCharm, the cursor on the function call, use Ctrl + Q shortcut to open Documentation window to see the comment function.

 2. Description automatically increase

 Place the cursor where you want the comment function name, the upper left corner there will be a small light bulb, click on the light bulb, the mouse to select the second.

IDE automatically add the following comments, the comment can be modified.

 

Use Ctrl + Q View Documentation Comments:

 

Guess you like

Origin www.cnblogs.com/dujinyang/p/11260644.html