Exercise 21: Functions can return something
You've learned to use = to name a variable, and to define a variable as a number or a string. And then we'll let you witness more miracles. What we're going to show you is how to use = and a new Python word return to set the variable to "the value of a function". There is one thing you need to pay attention to, but for the moment, let's write the following script:
1 defAdd (A, b):2 Print "ADDING%d +%d"%(A, b)3 returnA +b4 5 defSubtract (A, b):6 Print "subtracting%d-%d"%(A, b)7 returnAb8 9 defMultiply (A, b):Ten Print "multiplying%d *%d"%(A, b) One returnAb A - defDivide (A, b): - Print "dividing%d/%d"%(A, b) the returnAb - - - Print "Let's do some math with just functions!" + -Age = Add (30, 5) +Height = Subtract (78, 4) AWeight = Multiply (90, 2) atiq = Divide (100, 2) - - Print "Age :%d, Height:%d, Weight:%d, IQ:%d"%(age, height, weight, IQ) - - - #A Puzzle for the extra credits, type it in anyway. in Print "Here is a puzzle." - towhat = Add (age, subtract (height, multiply (weight, divide (IQ, 2)))) + - Print "that becomes:", What,"Can Do it by hand?"
View Code
Now we have created our own subtraction math function: add , &NBSP;subtract , < Span class= "Pre" >multiply , and divide . It is important that the last line of the function, such as &NBSP;add The last line is &NBSP;return a + b , which implements this function:
- We used two parameters when we called the function: a and b .
- We print out the functions of this function, here is the calculation of addition (adding)
- Next we tell Python to let it do a callback action: we return the value of a + b (return). Or you could say, "I'll add a and b together and return the results." ”
- Python adds two numbers and then, when the function is finished, it assigns the result of a + b to a variable.
As with many of the other things in this book, you need to slowly digest the content, step-by-step, and track what happened. To help you understand, this section of the plus points exercise will let you solve a puzzle, and let you learn something cool.
The results you should see
Bonus points Exercise
- If you're not sure about the return function, try writing a few functions yourself to get them to return some values. You can put anything that can be placed on the right side as the return value of a function.
- The end of the script is a puzzle. I use the return value of a function as a parameter to another function. I link them together, just like I write math equations. It may be a bit hard to read, but you'll know the result if you run it. Next, you need to try to make the same function as the expression in the normal way.
- Once you've solved the puzzle, try changing some parts of the function and see what happens. You can change it to a destination and let it output another value.
- Finally, turn it upside down and do it once. Write a simple equation and use the same function to calculate it.
This exercise may make you a little bit bigger, but it's time to take it as a game, and solving such puzzles is one of the pleasures of programming. You'll see a little puzzle like that later.
Exercise Exercises
1.
Stupid way to learn Python (21)