Skip to content

[Python] Tutorial(7) function

Last Updated on 2021-04-09 by Clay

The "function" is part of the programming language structure.

I think we can simply define it as "program components that can be called multiple times after being packaged."

Basically, if you have the opportunity to search the Internet for "clean code", you will see more explanations about function and class —— if the concept of these programming is clear, then you can choose to skip this section directly. It may be too simple for you, I am afraid that it will consume your time.

Of course, in Python's programming, there has always been a simplistic "scripting" and a "C" that uses function and class to wrap the function (sorry, these are my own definitions. XDDD can be used too seriously.)

However, even if you just think of Python as a "script-based" engineer who can perform the functions you want, you need to learn the use of functions —— because it saves a lot of time!


Define a function

In fact, the sample code in the previous tutorial used function, but I hope to explain here again how to define a function.

First, starting the most classic Fibonacci numbers, there will be a concept of recursion.

However, I would like a wait for a chance to mention "How to use Python for Sudoku problem solving".

Let's figure out the concept of "returning back".

Add a little knowledge:

"Fibonacci Sequence" redirects here. For the chamber ensemble, see Fibonacci Sequence (ensemble).
A tiling with squares whose side lengths are successive Fibonacci numbers
In mathematics, the Fibonacci numbers, commonly denoted Fn form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is,[1]
F{0}=0, F{1}=1
and
F{n}=F{n-1}+F{n-2}
One has F2 = 1. In some books, and particularly in old ones, F0, the "0" is omitted, and the Fibonacci sequence starts with F1 = F2 = 1.[2][3] The beginning of the sequence is thus:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, [4]

—— From Wikipedia

The above is Wikipedia's introduction to the Fischer series.

I have kept the original links. I am interested and welcome to read other related knowledge.

The three formulas in the middle are the definitions of the Fibonacci sequence.

Below, we use the "def" symbol to define the function based on these three expressions.

def fibo(n):
    if n == 0: return 0;
    elif n == 1: return 1;
    return fibo(n-1) + fibo(n-2)


We finished!

Huh? Someone must be very confused, so that the three mathematical formulas are completed.

That's right, the three lines in the function are exactly the same as the three mathematical formulas:

  • Define f(0) is 0
  • Define f(1) is 1
  • Define f(n) is f(n-1) + f(n-2)

The "def" command, as I just said, is an instruction to define a function. Keep it in mind!

The function "fibo" name can be taken by yourself, but it is recommended to make sense.

Otherwise, only when you start writing the program, you and God can understand it. After two or three weeks —— only God can understand it! (I often do this kind of thing)

Then the part of the "indentation" is the code defined in the function As long as the function is called, the code inside can be executed according to the value we pass.

The part of return is the function that returns the value that gives us the variable, and the actual exercise will be performed.

Let me explain the code.

I believe n == 0 and n == 1 after the first few courses, you must have been tired of it, so let me explain the part about return fibo(n-1) + fibo(n-2).

Simply put, when we are ready to return the return value, we call the fibo() function again! And this time, the n value of the function input is -1, -2 of the n value of the initial input.

This is actually the so-called "recursive", we will continue to call the smaller n value to execute the program, until it meets our program if n == 0 and if n == 1 will stop, no longer call yourself.

Maybe it will be clearer by the chart.
Suppose we entered fibo(5):

From the beginning of the function split to the end, fibo(0) returns a value of 0, only fibo(1) will return 1, then what will be the final result.

ans = fibo(5)
print(ans)


Output:

5

Ans is the final return value for us. Is the result just like everyone's guess?


So the above is our explanation of the function function. Finally let's take a look: you can write the function you want to execute multiple times, and then call this function every time you need it.

Believe me, this will be very convenient.


References


Read More

1 thought on “[Python] Tutorial(7) function”

  1. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my blog that automatically tweet my newest twitter
    updates. I’ve been looking for a plug-in like this for quite some time and was hoping maybe you would have some
    experience with something like this. Please let me
    know if you run into anything. I truly enjoy reading your blog and I
    look forward to your new updates.

Leave a ReplyCancel reply

Exit mobile version