I explained in the previous section that when defining a function, it is done as follows.
def Self-made function name(argument):
What is executed when the function is called
Last time, I didn't explain the ** argument ** in this, so I would like to explain it. In addition, a function originally returns something as a result of processing when a value is entered. We will also touch on ** return values **, such as returning values.
When defining and calling a function, you can pass ** arguments ** to the function for processing. Create a new chap06 </ font> and create a file with the file name samp06-02-01.py </ font> in it. Create it and write the following code. (The previous program has been slightly improved.)
samp06-02-01.py
def greet_func(name):
print('Running inside a function')
print(f'{name}San, Hello!')
print('From now on greet_Call the func function.')
name = input('Please enter your name:')
#greet_Call the func function
greet_func(name)
print('The processing inside the function is finished!')
[Execution result] </ font> Now call the greet_func function. Please enter your name: Taro Yamada Running inside a function Taro Yamada, Hello! The processing inside the function is finished!
I will explain the program. First, enter the name (this time **'Taro Yamada' **) and call the ** greet_func function **. Since ** name ='Taro Yamada' ** is set when calling, it is the same as ** greet_func ('Taro Yamada') ** when calling the function.
The part of ** name ** of ** greet_func (name) ** that is calling the function at this time is ** actual argument (Jitsuhikisu) </ font> > **.
Next is the execution within the called function below.
def greet_func(name):
print('Running inside a function')
print(f'{name}San, Hello!')
At this time, the value **'Taro Yamada' ** is assigned to the name of greet_func (name). And now within this function, because it has become a ** name = 'Taro Yamada' **, (''s {name}, Hello!' F) ** print to the point of ** ** name ** is assigned a ** 'Taro Yamada' **, it is output as "Taro Yamada, Hello!".
At this time, the part of ** name ** of ** greet_func (name) **, which is the function to be called, is ** formal argument (Karihikisu) </ font> * *Is called.
Let's look at another example. Create a new chap06 </ font> and create a file with the file name samp06-02-02.py </ font> in it. Create it and write the following code.
.06-02-02.py
def plus_func(a, b, c):
print('The sum is calculated in the function.')
print(a + b + c)
plus_func(3, -4, 1)
print('The processing inside the function is finished.')
[Execution result] </ font> The sum is calculated in the function. 0 The processing inside the function is finished.
This time, the actual argument is ** numeric ** instead of a string. The same is true for numbers, where we pass three values when calling the ** plus_func function **. Note that you can pass as many parameters as you like, not just one. And the numerical value passed to the formal argument is calculated in the plus_func function and the result is output.
As explained in Chapter 06-01, it is a function in mathematics.
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. In other words, if you substitute ** 2 ** for the actual argument ** x ** and perform the calculation, the result ** 14 ** will be returned. This returned value is called the ** return value **.
Please note that ** the return value is basically one **.
By the way, in the programs I have written so far, it is processed in the function and finished, and there is no return value. This time, I will explain how to return this result.
First, to return the return value, write the program as follows.
def Self-made function name(argument):
What is executed when the function is called
Processing result in the return function
The previous function,
f(x) = x^2+3x+4
Let's implement it programmatically. Create a new chap06 </ font> and create a file with the file name samp06-02-03.py </ font> in it. Create it and write the following code.
def calc_func(x):
print('Run inside a function')
f = (x ** 2) + (3 * x) + 4
##Returns the calculation result f
return f
#The actual argument is 2 when calling the function
f = calc_func(2)
print(f'Value returned from the function:{f}')
[Execution result] </ font> Run inside a function Value returned from the function: 14
First, ** 2 ** is specified as the actual argument of the calling function. It is specified in the formal argument x of the called function. Then, 2 is assigned to x in the function for calculation.
Next, the calculation result ** 14 ** is assigned to f, and that f is returned to the caller of the function. This value is called the ** return value **.
Then, the location of ** f = calc_func (2) ** becomes ** f = 14 **, so "value returned from the function: 14" is output.
The arguments and return values of a function are difficult to understand at first if it is a function that you have defined yourself, but do you know that even the built-in functions that you have used so far are actually used unknowingly? For example, in the case of ** len function **, when ** len ('hello') ** is set, **'hello' ** becomes ** actual argument **, and it is executed because it is 5 characters. The result ** 5 ** becomes the ** return value **.
Next time, I'd like to talk a little more about arguments and return values.