This article is written by a fledgling engineer who has been studying programming for about two months for the purpose of output. After biting ruby and js, I became interested in the trend python, so I started learning. This time I will write an article about python function definitions. This is a poor article, but I would appreciate it if you could point out any points that interest you! This article is based on the assumption that python3 and anaconda are installed on macOS.
A function is an instruction that executes a specified process and returns the result. By giving a name to the process you want to perform, you can call the process with that name. I can't understand it from words alone, so I'll actually look at the code.
The function definition is done in this way.
def function name():
What you want to do
Illustration
sample.py
def aisatu():
print("hello") #print("hello")The process is named aisatu
aisatu() #Call process by name
Execution result in the terminal
python sample.py
hello
By naming the process in this way, you can easily call the process. This time it is difficult to understand because it only outputs hello, but in the case of processing that needs to be done many times, It has the advantage of omitting the code and making the code easier to read.
An argument is a value given when calling a function. Take, for example, a function that adds two numbers.
sample.py
def tashizan(a,b):
print(a + b)
tashizan(1,3)
When you add two numbers, you need two numbers. The part (a, b) indicates that two numbers a and b are required, and when calling the function, two numbers are given in (1,3). The value in parentheses after the function name indicates the value required to call the function.
This is the end of this article. Even if I understood the function part, I found it difficult to verbalize and convey it. Efforts to verbalize will lead to the establishment of knowledge, so I would like to continue to do my best.
Previous article → https://qiita.com/shin12032123/items/89ecbff9257833eceff3 Next article → https://qiita.com/shin12032123/items/5f7d3db23962957d2bb3
Recommended Posts