[Python for Hikari-] Chapter 06-03 Functions (arguments and return value 2)

[Python] Chapter 06-03 Arguments and return value 2

In the previous section, we touched on arguments and return values, but there are other ways to specify arguments. This time, I would like to take a closer look at the case where there are multiple actual arguments and formal arguments.

Keyword arguments

First, let's talk about keyword arguments. Suddenly, create a file with the file name samp06-03-01.py </ font> in chap06 </ font>. , Write the code below. Note that ** last_name ** represents the surname and ** first_name ** represents the name.

samp06-03-01.py


def yamada_func(last_name, first_name, age):
    print(f'Full name:{last_name} {first_name},age:{age}age')


yamada_func('Yamada', 'Ichiro', 19)
yamada_func('Yamada', 'Jiro', 17)
yamada_func('Yamada', 'Saburo', 14)

[Execution result] </ font> Name: Ichiro Yamada, Age: 19 years old Name: Jiro Yamada, Age: 17 years old Name: Saburo Yamada, Age: 14 years old

First of all, although I did not explain in detail in the previous section, when calling the function, when passing the actual argument specified as follows to the formal argument, ** pass it from the left without changing the order ** I am. (**'Yamada' ** is never passed to the formal argument ** first_name **.) image.png

However, it is possible to specify which actual argument is assigned to which formal argument, as shown below. Please modify the program you wrote earlier as follows.

samp06-03-01.py


def yamada_func(first_name, age, last_name):
    print(f'Full name:{last_name} {first_name},age:{age}age')


yamada_func(first_name='Ichiro', age=19, last_name='Yamada',)
yamada_func('Yamada', 'Jiro', 17)
yamada_func('Yamada', 'Saburo', 14)

[Execution result] </ font> Name: Ichiro Yamada, Age: 19 years old Name: Jiro Yamada, Age: 17 years old Name: Saburo Yamada, Age: 14 years old

This time, the order of the actual argument and the formal argument is different, but ** If you specify the variable name of the formal argument, you can call the function without worrying about the order **. The method of specifying a variable for such an argument is called ** keyword argument **. Also, the program written at the very beginning of this section must be in order. The method of not specifying such a variable is called ** positional argument **.

Default argument

Earlier, I created a function program that outputs the name and age. It will be a similar program, but this time I will create a program that ** displays the attendance number, name and age of the same class of high school students **. Create a file with the file name samp06-03-02.py </ font> in chap06 </ font> and use the following code Please write.

samp06-03-02.py


def classmate_func(No, last_name, first_name, age):
    print(f'Attendance number:{No},Full name:{last_name} {first_name},age:{age}age')

print('Call the function.')
classmate_func(7, 'Kawasaki', 'Jyun Pei', 17)
classmate_func(13, 'Tatehara', 'Haruki', 17)
classmate_func(21, 'Matsumoto', 'Yuki', 17)

[Execution result] </ font> Call the function. Attendance number: 7, Name: Junpei Kawasaki, Age: 17 years old Attendance number: 13, Name: Haruki Tatehara, Age: 17 years old Attendance number: 21, Name: Yuki Matsumoto, Age: 17 years old

As for the output result, the program is almost the same as before, so I think there is no problem. What I would like you to see here is the age (** 17 years old **). Under this condition, it is ** "in the same class of high school students" **, so basically the ages should be the same. (It is possible that the age may differ depending on some circumstances, such as birthday circumstances, but I will touch on that later.)

In such cases, it is troublesome to specify the age each time, so you can specify it as the default value in advance. Please modify the program you wrote earlier as follows. * </ font> Also note that the number of actual arguments at the caller of the function has been reduced to three)

samp06-03-02.py


def classmate_func(No, last_name, first_name, age=17):
    print(f'Attendance number:{No},Full name:{last_name} {first_name},age:{age}age')

print('Call the function.')
classmate_func(7, 'Kawasaki', 'Jyun Pei')
classmate_func(13, 'Tatehara', 'Haruki')
classmate_func(21, 'Matsumoto', 'Yuki')

[Execution result] </ font> Call the function. Attendance number: 7, Name: Junpei Kawasaki, Age: 17 years old Attendance number: 13, Name: Haruki Tatehara, Age: 17 years old Attendance number: 21, Name: Yuki Matsumoto, Age: 17 years old

The result is the same as before, but ** 17 ** is not specified in the place of the actual argument. However, each output result specifies ** "age: 17 years old" **. Focusing on the formal argument of the ** classmate_func function ** that is called, this is classmate_func (No, last_name, first_name, ** age = 17 **), and this ** age = 17 ** , The value of age = 17 is already entered in the function.

These arguments are called ** default arguments ** </ font>.

Also, the default argument is ** only the default, so it can be overwritten **. Please modify the program you wrote earlier as follows. * </ font> The actual argument of "Haruki Tatehara", the caller of the function, has been changed.

samp06-03-02.py


def classmate_func(No, last_name, first_name, age=17):
    print(f'Attendance number:{No},Full name:{last_name} {first_name},age:{age}age')

print('Call the function.')
classmate_func(7, 'Kawasaki', 'Jyun Pei')
classmate_func(13, 'Tatehara', 'Haruki', 18)
classmate_func(21, 'Matsumoto', 'Yuki')

[Execution result] </ font> Call the function. Attendance number: 7, Name: Junpei Kawasaki, Age: 17 years old Attendance number: 13, Name: Haruki Tatehara, Age: 18 years old Attendance number: 21, Name: Yuki Matsumoto, Age: 17 years old

Finally

This time I touched on keyword arguments and default arguments. When there are multiple arguments, it is easy to forget which argument corresponds to it, so if you use keyword arguments in such cases, you will not forget. Also, the default arguments may occasionally appear when writing AI programs, so keep them in mind.

In the Fundamental Information Technology Engineer Examination, keyword arguments and default arguments may be asked, so be sure to hold them down if you plan to take the exam.

Return to [Table of Contents Link]

Recommended Posts