python function
What are Python functions?
What are Python functions? I study alone or take a class at school, but most of the engineering colleges or computer sciences do not know exactly what a function is. It’s not the first time I’ve been so embarrassed by skipping a few lectures. Of course, I’m not an expert, but I’ll try to talk about what I know. If there are any errors or questions, please let me know by email or comment.
A Python function may have different results depending on the input value, but the logic itself is put in a box in the same case so that it can be reused over and over again, we call it a function.
Therefore, if we manage one logic by grouping it into a function without inefficient ctrl + c, ctrl + v, we can write code comfortably and the code becomes neat, so we use functions.
If you think about making chairs in a factory, you’ll be comfortable. When making a chair, you need to make the backrest, legs, and seating part, but how inefficient would it be if you made 4 chair legs and made each one separately? right?
So we extract the same thing with automation. You can think of this as a function. automated machine. In other words, a machine that produces the same result can be called a function, and the chair leg can be called a return value. If you can make a chair leg of another size in one machine by inputting the size and shape of the chair seat in the machine, it can be called a function with parameters.
There are various functions in the C language, but in Python, the keyword def is used to start a function. If you write def, it is defined that the function will start after def. Also, we need to know that functions have inputs and outputs. You need to know this.
Comparing with the article above, whether you just want the legs of a chair, or something with wheels, it could be the legs of a rocking chair. We use this as an input value, and according to the input value, the desired leg of a chair is called an output value. Depending on the function, some functions have inputs, some functions have outputs, and some functions have both or neither.
The important thing is that functions are very important in Python. Functions can be passed around just like any other variable, and provide a very powerful solution. We will look at some examples of using these functions later in this chapter.
There is no need to be tired and struggling already. I have no doubt that if you read to the end without giving up once, your fear of language will disappear.
Function usage
As you know in Python, line alignment is very important, and if the paragraphs don’t match, you’ll get an error. It would be good to keep that in mind and study while writing the code. Below is the basic framework of the function.
def function_name ():
sentence to be executed
....
....
As you can see above, the syntax of the def function does not matter. In this way, if the function name comes after def, parentheses (), and then a colon (:), insert indent to create a function. After that, you can use the desired sentence by indenting it below the function. But when we do some real coding and get more and more proficient, we’ll see. In real coding life, you cannot use the syntax def. When we learn a language from another country, simple communication is possible just because we have learned a few words, but it doesn’t mean that we can do free-talk. In order to communicate with each other, you need to know how to use the word and when to say it. Therefore, we will tell you when and in what situations to use the function called def.
def func():
print('HelloWorld!')
In this way, we defined a function called func. However, just defining a function is not used. How to use a function? To call a function, enter the name of the function and up to () parentheses to call it. Let’s call the function we’ve created once or three times.
If you write in this way, the logic inside the function will work as the function is called. We put the print(“HelloWorld!”) command inside the function func so that the string “HelloWorld!” is printed every time the function is called.
This way you can see the results. If it is not a function and you repeat the same thing inefficiently (?), you have to repeat print(“ “) 3 times. It’s ok for now as it is one line, but if the logic inside the function becomes complicated with multiple lines, if you repeat it N times a day, the code will become really messy as well as readability.
So, let’s find out when to use a function with an example.
Purpose of function 1. It is used to shorten long code.
Functions are not a mandatory syntax, nor are they used to make my code look nice. If there is a long code that is used frequently, you can use it when you want to abbreviate it into one short word.
print('Hello,')
print("I'm explaining the purpose")
print('of the function')
print('Do you know')
print('when to use a function?')
print('From now on,')
print('get to know if they do not know you')
Let’s say we need this long code very often. Then you have to copy and paste this long code every time, right? If it bothers you, you can shorten this long code into one short word by using the function syntax.
Write the function syntax like this
def short_name() :
Long codes to be abbreviated 1
Long codes to be abbreviated 2
Long codes to be abbreviated 3
Long codes to be abbreviated 4
Long codes to be abbreviated 5
Long codes to be abbreviated 6
Long codes to be abbreviated 7
You can freely name the name to be abbreviated, and put all the code to be abbreviated in indent. Then, if you just write the abbreviated name() in the future, all the long code fed with indent will be executed. Now that you’ve learned it, try abbreviate the two lines of code above. How can I abbreviate?
Check answer
def HI():
print('Hello,')
print("I'm explaining the purpose")
print('of the function')
print('Do you know')
print('when to use a function?')
print('From now on,')
print('get to know if they do not know you')
HI()
I’ll end it here today. If the story is too long, you will not be able to concentrate, but you will not be able to know what you have studied above. Therefore, in the next article, the use of important functions 2 hat making, leaving leather, function with only input, function with only return value, function with both input and return value, python function example, when not using multiplication table function, multiplication table function When using , Python function application
댓글남기기