[PYTHON] [Thorough explanation] How to understand functions (methods) that beginners must see

It is no exaggeration to say that the most important function or method for getting started with programming (hereinafter both are called functions).

What do you think of when you hear the word function?

In general, I think it's a word you hear in math classes.

So why did you come to call a method a function in the programming world?

That's because the concepts are very similar to mathematical functions.

So let's first look at mathematical functions. People who are allergic to math will not refuse and will not talk about difficult things, so I hope you will read on.

First of all, what is a function?

A function always returns one y (solution) when given an arbitrary x. It's difficult with just words.

So what are some typical mathematical functions?

The first function I think of is

y = ax + b

is. This is called a linear function, but since a and b are constants (the number can be determined arbitrarily), be specific.

y = 2x + 3

Let's look at.

If you specify x in this function, a certain number will give you the answer. For example, if x = 3, the value of y would be 9. This can be applied to various numbers, isn't it?

So a function is one that returns a single Y when given a specific X.

image

Something like this function is not limited to mathematics.

For example, let's say you have a chef who cooks delicious food when you give it ingredients. In a sense, this chef works like a function.

Specifically, if you give this chef "carrots, potatoes, beef, curry powder" as X, this chef will return the Y value of curry.

image

The general function outline is that if you give X like this, it will return Y.

Therefore, it can be said that programming functions (methods) are the same.

However, in programming, the above X is called an argument, and Y is called a return value.

image

Let's take a look at the actual code.

I'm using Ruby code this time, but this concept doesn't change in major programming languages.

Now let's create a method that adds the given two numbers (arguments)

#Method name is add
#The argument is a, b

#I decide what method to make and what arguments to take, so
#You can freely decide the name of the method name and the name of the argument.

#Method definition (creation)
def add(a, b)
  return a + b
end

#Method call (use)
add(3, 2)
# =>The return value will be 5

The diagram of this code is as follows

The newly introduced concept here

"The calling code itself turns into a return value"

about it. This is a very important concept, so make sure you understand it.

In other words

result = add(2, 3)

The code is

result = 5

It is exactly the same as the code.

There are several reasons why you should write add (2,3) instead of writing 5 directly.

First of all, since this example uses the method of addition,

You can predict the return value as soon as 5 in your head, but if this is a very complicated process

I have to use the method because the return value is unpredictable.

Next, in this case, the arguments (a, b) are predetermined.

This is actually a variable, so you can't predict the answer before using the method.

That's it for the basic concept of methods.

Programming uses a lot of functions (methods).

It is no exaggeration to say that most of what I write is a function.

We often define functions, but most of the time we use functions created by others.

At that time, if you look at the function with the concept of what is required as an argument and what kind of return value is given

You can use it without having to understand it easily and understand the details of the function.

So, when using or defining a function (method), pay attention to its arguments and return value.

Recommended Posts

[Thorough explanation] How to understand functions (methods) that beginners must see
[Python] Understand how to use recursive functions
Understand how to use django-filter
[For beginners] Super introduction to neural networks that even cats can understand