From this chapter, we will explain about Functions. Functions are a concept that appears in any programming language, not just Python. Let's hold it down.
Speaking of functions, I think I learned linear and quadratic functions in mathematics when I was in junior high school and high school. For example, the following quadratic function,
f(x) = x^2+3x+4
When there is, if x = 2
f(2) = 2^2+3.2+4
=14
I was able to ask.
Even in a program, if you enter some value like x = 2 above, you can define a function that ** returns a value **. This time I would like to see how to create that function.
In fact, you've probably touched on things like ** print, int, str, len, sorted, range **, etc. in the past. Let's actually see it. Enter the following code from the ** Python Console **.
>>> str = 'hello'
>>> len(str)
5
>>> ls = [1, 2, 3, 4, 5]
>>> len(ls)
5
I explained it with ** len function ** now, but it is the same as specifying x = 2 above, and if you put a string variable or list variable in len, the length (or number of elements) will be returned. I will come.
Not only ** len function **, but ** print function, int function, str function, sorted function, range function ** are functions originally prepared in Python. These functions are called ** built-in functions **. There are many built-in functions. You can check the built-in functions and usage from the links below.
https://docs.python.org/ja/3/library/functions.html
In addition, using a function is often referred to as ** "calling a function" **.
I mentioned the built-in functions prepared in advance, but you can also create and use the functions yourself. This is called ** Function Definition **.
By using function definitions, long programs can be divided into parts for each role, and readability is improved.
There are the following methods to actually define the function.
def Self-made function name(argument):
What is executed when the function is called
The first ** def ** means a definition, such as defining, and is a description that defines a function. Then, define the function name you created, add **: ** (colon) as in the case of if statement and while statement, and then describe the processing contents in the function.
In addition, about the above ** argument (hikisu) **, this is the place to enter the value such as ** x = 2 ** earlier. I would like to explain this next time.
I'll show you an example of a function from now on, but first I'd like to explain it without any arguments. Create a new chap06 </ font> and create a file with the file name samp06-01-01.py </ font> in it. Create it and write the following code. Simply put, you can think of it as a function for greeting.
.samp06-01-01.py
def greet_func():
print('Running inside a function')
print('Hello!')
Now, do the above.
[Execution result] </ font>
When I actually run it, nothing is displayed. Just defining the ** greet_func function ** this time does not make sense, and you need to specify the ** calling process ** such as "Please move the ** greet_func function **".
Add the following to your program.
.samp06-01-01.py
def greet_func():
print('Running inside a function')
print('Hello!')
print('From now on greet_Call the func function.')
#greet_Call the func function
greet_func()
print('The processing inside the function is finished!')
[Execution result] </ font> Now call the greet_func function. Running inside a function Hello! The processing inside the function is finished!
As mentioned above, a function makes no sense unless it is defined and called. Of course, the built-in functions ** print function ** and ** len function ** are also available, but you can't use them unless you call them. It will be the same as that. This time, I am calling the original ** greet_func function **.
Basically, functions are often written first. Function part when executed
def greet_func():
print('Running inside a function')
print('Hello!')
Is skipped and ** "Call the greet_func function now." ** is output.
Then, as shown in the flowchart below, the function is called, and when the processing inside the function is completed, it returns to the caller, the normal processing starts, and "The processing inside the function is finished!" Is output.
You can also create multiple functions. Enter the code below. Create a file with the file name samp06-01-02.py </ font> in the chap06 </ font> folder, and create the following file. Write the code.
.06-01-02.py
def morning_func():
print('The morning function was called.')
print('Good morning!')
def afternoon_func():
print('The daytime function was called.')
print('Hello!')
def night_func():
print('The night function was called.')
print('Good evening!')
#Where to enter numbers
judge = int(input('Enter 0 for morning, 1 for noon, 2 for night:'))
if judge == 0:
morning_func()
elif judge == 1:
afternoon_func()
elif judge == 2:
night_func()
else:
print('Please enter the correct value.')
[Execution result] </ font> Enter 0 for morning, 1 for noon, 2 for night: 2 The night function was called. Good evening!
I wrote 3 functions this time, but you can actually write any number of ** functions **. In the processing content this time, the function to be called is changed according to the entered numerical value. If the value you entered is other than the specified value (this time it is set to 5), you will be prompted to re-enter it.
If you enter the correct value (this time it is set to 2), the function corresponding to it will be called and processed in the function. This time, the night function (** night_func **) is called, so the processing inside the night_func function is executed.
Below is a flow chart of the processing details. (This time, the processing inside the function is omitted.)
This time, I touched on the concept of functions, which appears in every programming language. You might have thought that the function would use a function that you have already prepared, but you can actually create your own. The function I touched on this time is simple, but next time I would like to touch it a little deeper.