Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,030 --> 00:00:06,660
Once you start writing code, you will find out that parts of the code repeat themselves and this is
1
2
00:00:06,840 --> 00:00:08,050
normal in programming.
2
3
00:00:08,070 --> 00:00:11,490
In fact, it's the way it happens. When you find these patterns,
3
4
00:00:11,520 --> 00:00:14,520
you can make them reusable by creating a function.
4
5
00:00:14,610 --> 00:00:21,210
A function is a separate piece of code that can be called on demand by specifying its name in the execution.
5
6
00:00:21,240 --> 00:00:22,450
Let's look at the code.
6
7
00:00:22,500 --> 00:00:24,930
So here we have defined a function add().
7
8
00:00:25,020 --> 00:00:31,560
To create the new function, we need to use the operator func in GDScript and then the name of the
8
9
00:00:31,560 --> 00:00:32,070
function.
9
10
00:00:32,070 --> 00:00:40,260
And we do need to use the parenthesis, but they can be empty or we can specify additional parameters.
10
11
00:00:40,380 --> 00:00:46,890
Don't forget to add the colons after each function implementation. In the function body
11
12
00:00:47,460 --> 00:00:53,400
we can do something with the provided values and in this case we just add them.
12
13
00:00:53,490 --> 00:00:59,400
And we used another operator called return, which will return the final value.
13
14
00:00:59,460 --> 00:01:03,960
And this value can be used in the previous function that called this one.
14
15
00:01:03,990 --> 00:01:09,030
In this case, I'll be saying adding one plus one and then calling this function.
15
16
00:01:09,060 --> 00:01:14,220
This will add these two elements and will give the result to be printed in the console.
16
17
00:01:14,250 --> 00:01:17,070
Keep in mind that now we can use this.
17
18
00:01:17,070 --> 00:01:21,810
Add as many times as we like to add two values.
18
19
00:01:22,170 --> 00:01:24,460
But this can be a move function.
19
20
00:01:24,480 --> 00:01:26,860
This can be a fire projectile function.
20
21
00:01:26,880 --> 00:01:28,560
This can be whatever you want.
21
22
00:01:28,590 --> 00:01:35,130
There is also another type of function called recursive function, and the recursive function is just
22
23
00:01:35,130 --> 00:01:40,890
like a regular function with the one spin that it calls itself in its own body.
23
24
00:01:40,920 --> 00:01:46,410
As you might imagine, if you've seen the Inception movie, this can lead to infinite execution.
24
25
00:01:46,410 --> 00:01:47,790
And we don't want that.
25
26
00:01:47,850 --> 00:01:52,740
So remember, if you ever implement a recursive function, then you need to make sure that you have
26
27
00:01:52,740 --> 00:01:54,600
something called the exit condition.
27
28
00:01:54,600 --> 00:02:01,650
So the exit condition is usually an IF statement that checks a final condition to break this recursive
28
29
00:02:01,650 --> 00:02:02,220
pattern.
29
30
00:02:02,520 --> 00:02:06,570
This is important to prevent the function running indefinitely.
30
31
00:02:06,600 --> 00:02:12,870
Recursive functions are useful in some particular algorithm implementations in this course.
3288
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.