# 4 [python] Basics of functions

Introduction

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.

1 What is a function in the first place?

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.

2 Function definition

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.

3 arguments

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.

At the end

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

# 4 [python] Basics of functions
Basics of Python ①
Basics of python ①
#Python basics (functions)
Python basics: functions
Basics of Python scraping basics
Basics of python: Output
Python basics ⑤
Python basics
Python functions
python: Basics of using scikit-learn ①
Python basics ③
Python basics
Python basics
Python basics
Python basics ③
Python basics ②
Python basics ②
Basics of Python × GIS (Part 1)
Basics of Python x GIS (Part 3)
Getting Started with Python Basics of Python
Review of the basics of Python (FizzBuzz)
Basics of Python x GIS (Part 2)
About the basics list of Python basics
Learn the basics of Python ① Beginners
Python basics: list
Introduction of Python
Python basics memorandum
#Python basics (#matplotlib)
Python CGI basics
Python basics: dictionary
Python slice basics
#Python basics (scope)
#Python basics (#Numpy 1/2)
#Python basics (#Numpy 2/2)
[Introduction to Data Scientists] Basics of Python ♬ Functions and classes
[Beginner] Python functions
Python array basics
Python profiling basics
Python Easy-to-use functions
Python #Numpy basics
#Python basics (class)
Python basics summary
Introduction of Python
Basics of binarized image processing with Python
Python: Basics of image recognition using CNN
[Learning memo] Basics of class by python
I didn't know the basics of Python
The basics of running NoxPlayer in Python
[Basics of python basics] Why do __name__ == "__main__"
[Python] Chapter 02-04 Basics of Python Program (About Comments)
[Python] Chapter 02-03 Basics of Python programs (input / output)
[Introduction to Data Scientists] Basics of Python ♬
[Python3] Understand the basics of file operations
[Introduction to Data Scientists] Basics of Python ♬ Functions and anonymous functions, etc.
Python Beginner's Guide (Functions)
List of python modules
Python basic course (12 functions)
Python basics ② for statement
Python: Unsupervised Learning: Basics
Unification of Python environment