Learning goals

  1. Understand the use of magic blocks – custom functions
  2. Learn about the result feedback of magic blocks – custom function return values
  3. Understand the raw material transfer of magic blocks – custom function pass parameters
  4. Learn about the types of magic blocks – classes and objects

recommended

1. The highly acclaimed Comprehensible C Language Primer

directory

If programming is magic, then I will make you understand the use of a magic wand. I will make you understand the use of magic wand. If programming is magic, the basic data types of the magic box list, dictionary, and basic data types of the magic box. “If the programming is the zero basis of magical can read the Python tutorial” – (5) I’m magic should have a sense of logic Article 6: “if the programming is the zero basis of magical can read the Python tutorial” – (6) lean more powerful magic This series of tutorials will be in the form of a “magical world”, welcome all of you support.

Welcome everyone to pay attention to the public number, the public number every 1024 and 1024 times will be lucky draw a mechanical keyboard +IT books 2 copies of yo ~ and the public number will be updated faster yo.

1. Understand the building blocks in the magical world

We have learned some basic Python programming from the previous chapters. However, when we write too much code, the code will be messy and the overall logic is not easy to view. If there is an error, we need to go through many procedures to troubleshoot it. Then, part of our code is combined into code by building blocks. If the weak part fails, it will quickly locate the error in which building block. At this time, it only needs to modify the code in that building block to correct the error. Does this make our code structurally clear and easy to fix bugs? The answer, of course, is right.

1.1 Understand the writing of custom functions

When we write an ordering system, we often prompt the menu bar for the user to choose. If the user enters another page and needs a new menu prompt when returning to the first page, we need to output it again. Is that particularly troublesome? We can take a look at the following example:

print('Please select the food number you want to eat and go in and pay')
print('1. Kung Pao Chicken ')
print(2. Spicy Chicken Wings)
print('3. Sticky rice ')
print('3. Dumplings')
print('4. Shaxian Classic Three-piece set ')
print('5. Multifarious meatballs')

c=int(input('Please enter menu number:'))
if c:
	print('Connecting to pay... ')
	b=input('Any key returns... ')
	if b:
		print('Returning to... ')

print('Please select the food number you want to eat and go in and pay')
print('1. Kung Pao Chicken ')
print(2. Spicy Chicken Wings)
print('3. Sticky rice ')
print('3. Dumplings')
print('4. Shaxian Classic Three-piece set ')
print('5. Multifarious meatballs')
Copy the code

The above examples are only used to simulate the situation, and the code is not used as a reference for the function. At the beginning of the code, the prompt menu content, the code is as follows:

print('Please select the food number you want to eat and go in and pay')
print('1. Kung Pao Chicken ')
print(2. Spicy Chicken Wings)
print('3. Sticky rice ')
print('3. Dumplings')
print('4. Shaxian Classic Three-piece set ')
print('5. Multifarious meatballs')
Copy the code

Prompt content after receiving user input a value. User input value, where any value can be entered, to judge the writing of the code:

if c:
Copy the code

The above code evaluates the C variable, as long as it is not empty, then any number will work. In Python, empty values are ultimately judged to be False and True if any. Then simulate the connection to the payment system, input a value at will for a long time and return after confirmation:

c=int(input('Please enter menu number:'))
if c:
	print('Connecting to pay... ')
	b=input('Any key returns... ')
	if b:
		print('Returning to... ')
Copy the code

Then reprint the menu bar. This is obviously duplicate code for the menu bar, used twice; Does this code seem too cumbersome? It’s just visually difficult. But don’t worry, Python provides custom functions to solve this problem. A custom function represents a function block that can be defined by itself. This function block can be used repeatedly. This function block can be composed of multiple codes.

Now let’s define the menu bar as a custom function. The syntax for defining a custom function begins with def, followed by a space; A space followed by a string of characters indicates the name of the function block; Followed by a bunch of parentheses followed by a colon. The code is as follows:

def caidan() :
Copy the code

The above code starts with def, which means I’m going to write a custom function, followed by the name caidan, followed by a bunch of parentheses, and terminated by a colon; Next, we need to write the function code inside the function. At this point, we need to have a uniform indentation format, at least one:

def caidan() :
	# here is the code for caidan
	# here is the code for caidan
Copy the code

We can simply copy and paste the entire menu tip code into our custom caidan function:

def caidan() :
print('Please select the food number you want to eat and go in and pay')
print('1. Kung Pao Chicken ')
print(2. Spicy Chicken Wings)
print('3. Sticky rice ')
print('3. Dumplings')
print('4. Shaxian Classic Three-piece set ')
print('5. Multifarious meatballs')
Copy the code

Is that correct? Wrong! Remember that when you customize a function, you need to write the function and then indent the structure in the name of the function; So the correct way to write this code is as follows:

def caidan() :
	print('Please select the food number you want to eat and go in and pay')
	print('1. Kung Pao Chicken ')
	print(2. Spicy Chicken Wings)
	print('3. Sticky rice ')
	print('3. Dumplings')
	print('4. Shaxian Classic Three-piece set ')
	print('5. Multifarious meatballs')
Copy the code

This is the custom function function has been written, so how to use it? Pretty simple, you remember how print works? Print and input are functions that come with the system; They are used by enclosing a pair of parentheses after the function name. Such as input (); We write our own functions to use the same method, the name of the function after a pair of parentheses to complete the use of custom functions, such as caidan(). Take a look at the full code for this section:

def caidan() :
    print('Please select the food number you want to eat and go in and pay')
    print('1. Kung Pao Chicken ')
    print(2. Spicy Chicken Wings)
    print('3. Sticky rice ')
    print('3. Dumplings')
    print('4. Shaxian Classic Three-piece set ')
    print('5. Multifarious meatballs')

caidan()
c=int(input('Please enter menu number:'))
if c:
	print('Connecting to pay... ')
	b=input('Any key returns... ')
	if b:
		print('Returning to... ')

print('Please select the food number you want to eat and go in and pay')
print('1. Kung Pao Chicken ')
print(2. Spicy Chicken Wings)
print('3. Sticky rice ')
print('3. Dumplings')
print('4. Shaxian Classic Three-piece set ')
print('5. Multifarious meatballs')
Copy the code

The above code defines the custom caidan function in the header and writes functions in the caidan function. Caidan () = caidan() = caidan() = caidan() = caidan() = caidan();

print('Please select the food number you want to eat and go in and pay')
print('1. Kung Pao Chicken ')
print(2. Spicy Chicken Wings)
print('3. Sticky rice ')
print('3. Dumplings')
print('4. Shaxian Classic Three-piece set ')
print('5. Multifarious meatballs')
Copy the code

The result of the code in the function should be displayed when the program is running:

If caidan() is used instead of caidan(), we can cancel the menu at the bottom of the code. The code is as follows:

def caidan() :
    print('Please select the food number you want to eat and go in and pay')
    print('1. Kung Pao Chicken ')
    print(2. Spicy Chicken Wings)
    print('3. Sticky rice ')
    print('3. Dumplings')
    print('4. Shaxian Classic Three-piece set ')
    print('5. Multifarious meatballs')

caidan()
c=int(input('Please enter menu number:'))
if c:
	print('Connecting to pay... ')
	b=input('Any key returns... ')
	if b:
		print('Returning to... ')

caidan()
Copy the code

Running results:



The results are consistent, but with much less code, it looks cleaner.

1.2 Understanding custom functions with Parameters

Remember how we did the addition of two numbers in the last few videos? The requirement is that the user enters two integers, and the computer automatically sums them up and displays them to tell us. We can use the function to do this, so that we can call the function directly when we need to use the function later.

First we define a function called jiafa:

def jiafa() :
Copy the code

This function takes two values, it just does addition, so how do you write it? You need to add two parameters, parameter is the number you need to “calculate”; As usual you use the rice cooker, the parameters of the rice cooker can be rice and water, because the function of the rice cooker is to cook, how to cook without rice and water? The same goes for our two-number addition function. How do we calculate without numbers? So we need to pass in two values, and these two values are parameters. Parameters are written in parentheses after jiafa; For example, number 1 variable :s1, number 2 variable :s2, then the code is:

def jiafa(s1,s2) :
Copy the code

This means that the function can take two arguments, or more, just as an example. Passing multiple arguments requires a comma to separate the arguments. After receiving the number, we can then begin to evaluate these two variables. The sum of the two variables is:

print(s1+s2)
Copy the code

Jiafa function code:

def jiafa(s1,s2) :
	print(s1+s2)
Copy the code

So how do you use it? Remember how print passed parameters? Print needs to pass the displayed value into parentheses; The argument passing of custom functions is similar. We pass in two numbers, a 1 and a 10, and write:

jiafa(1.10)
Copy the code

The complete code is as follows:

def jiafa(s1,s2) :
	print(s1+s2)

jiafa(1.10)
Copy the code

The running results are as follows:

Here you see, input 1 and 10, separated by commas, where the value of 1 is assigned to S1 and the value of 10 is stored in S2. This is the sum of s1 and S2, so the output is 11.

So let’s modify the code to use input to receive the two values we entered and evaluate them:

def jiafa(s1,s2) :
	print('The sum of two numbers equals:',s1+s2)

a=int(input('Enter a number :'))
b=int(input('Please enter another number :'))
jiafa(a,b)
Copy the code

The running results are as follows:

1.3 Learn about custom functions with return values

Now all of a sudden, I don’t want to output in the function, I just want to get the result, I want to output or not output according to the situation, so how do I implement this requirement? Very simple, we just return the evaluated value, so that our function does the calculation, not the output; Output or not according to the situation at that time, flexible use.

Return the last line of a custom function using return, followed by a space, followed by a value. The result is the value returned by return. For example, we modify the code in 1.2:

def jiafa(s1,s2) :
    return s1+s2
Copy the code

Compared with the original program output less, more than a return, and return followed by a space, space followed by the expression of the addition of two variables; This expression is not a direct value, but don’t worry, the expression will eventually be automatically evaluated to a value that will be returned by return. It doesn’t matter if you don’t understand, we can write:

def jiafa(s1,s2) :
	r=s1+s2
    return r
Copy the code

The variable r receives the value of the addition of S1 and s2 and returns the same value for r. So we can call a function in our code and assign the result that the function returns to a variable:

c=jiafa(a,b)
Copy the code

After jiafa has run all the code in its function, it returns a value that will be given to the C variable. Print c:

print('The result of addition is:',c)
Copy the code

Or, instead of using a variable to receive the output, use the return value of the function directly:

print('The result of addition is:',jiafa(a,b))
Copy the code

The final code is as follows:

def jiafa(s1,s2) :
    return s1+s2

a=int(input('Enter a number :'))
b=int(input('Please enter another number :'))
print('The result of addition is:',jiafa(a,b))
Copy the code

The running results are as follows:

Second, the summary

  1. Understand that custom functions need to be defined using def and that code content needs to be indented
  2. Learn how to use custom function arguments and separate multiple arguments with commas
  3. Learn about the return value of a custom function using return