WEBVTT 00:00:07.330 --> 00:00:10.440 In this video you're going to learn Ph.D. functions. 00:00:10.500 --> 00:00:14.360 So as we have covered in the javascript section we know that functions. 00:00:14.490 --> 00:00:20.510 They are used to replace a certain piece of code does we want to call it many times. 00:00:20.550 --> 00:00:25.440 So rather than writing this piece of code over and over we just place it inside the function. 00:00:25.620 --> 00:00:30.080 And whenever we need that piece of code we just call the function instead. 00:00:30.210 --> 00:00:36.480 So let's create our first function. 00:00:36.630 --> 00:00:39.530 The syntax is similar to Javascript. 00:00:39.570 --> 00:00:44.290 So we're going to use the function keyword followed by the name of the function. 00:00:44.460 --> 00:00:48.530 We're going to call it welcome me. 00:00:48.630 --> 00:00:54.960 Now then we're going to have parenthesis Where are we going to place any arguments for the function 00:00:54.960 --> 00:00:55.200 . 00:00:55.200 --> 00:00:59.340 So this one is not going to have any arguments or any parameters. 00:00:59.340 --> 00:01:04.510 So we're going to leave it's empty Are they going to have curly brackets. 00:01:04.550 --> 00:01:08.700 We're going to write the piece of code to execute. 00:01:09.540 --> 00:01:09.830 OK. 00:01:09.840 --> 00:01:11.940 So for this one let's write something simple. 00:01:12.060 --> 00:01:15.540 Let's echo a paragraph 00:01:18.710 --> 00:01:20.110 and this. 00:01:20.520 --> 00:01:26.330 For example Welcome to my Web site. 00:01:28.000 --> 00:01:31.040 Save we save and refresh. 00:01:32.700 --> 00:01:34.510 Nothing happens. 00:01:34.620 --> 00:01:40.710 And the reason why is because we have just defined the function we haven't called the function yet. 00:01:41.010 --> 00:01:45.930 So if you want the piece of code to be executed we'll need to call the function so to call the function 00:01:45.970 --> 00:01:55.530 we can to use the name of the function followed by parenthesis. 00:01:55.620 --> 00:02:02.930 Any arguments or promises that go inside the pregnancy's for this function. 00:02:02.940 --> 00:02:04.300 We don't need any arguments. 00:02:04.340 --> 00:02:14.520 OK so if we save and refresh now we can see that we have called the function and the code has been executed 00:02:14.520 --> 00:02:15.390 . 00:02:15.480 --> 00:02:16.710 All right. 00:02:17.120 --> 00:02:17.570 OK. 00:02:17.700 --> 00:02:26.070 Now we're going to define another function and this time. 00:02:26.140 --> 00:02:36.570 OK so let's just leave the definition of the functions at the top and all the the calls at the bottom 00:02:36.590 --> 00:02:36.760 . 00:02:36.930 --> 00:02:41.780 So the second function is going to have a couple of provinces. 00:02:42.120 --> 00:02:45.590 So we're going to call it welcome user. 00:02:47.100 --> 00:03:00.060 And we're going to have first Bar-Mitzvah name and the second Brahmi to say credits. 00:03:00.830 --> 00:03:02.270 OK. 00:03:03.300 --> 00:03:04.850 After that we're going to have curly brackets. 00:03:04.850 --> 00:03:13.100 We're going to have the code to be executed and this time we want to echo. 00:03:13.490 --> 00:03:16.600 We can have a paragraph any side paragraph we're going to have. 00:03:16.800 --> 00:03:22.290 Hi followed by the name and then we're going to say welcome to my Web site. 00:03:22.380 --> 00:03:26.070 Your credit is we're going to use this credit. 00:03:26.230 --> 00:03:35.260 OK OK so we're going to have a paragraph and then inside the program we're going to write. 00:03:35.730 --> 00:03:36.500 Hi. 00:03:36.720 --> 00:03:37.050 OK. 00:03:37.050 --> 00:03:40.740 Followed by name. 00:03:40.920 --> 00:03:42.180 OK. 00:03:42.530 --> 00:03:50.640 Since we are using double quotes I always drink we can just use the variables straightaway inside the 00:03:51.190 --> 00:03:52.300 records. 00:03:52.680 --> 00:03:53.470 OK. 00:03:53.610 --> 00:04:13.350 And I say welcome to my web sites and say your credits is then followed by the credits variable. 00:04:14.050 --> 00:04:15.870 OK. 00:04:16.060 --> 00:04:21.250 And then dollars. 00:04:23.100 --> 00:04:26.870 OK so now let's call the function. 00:04:27.680 --> 00:04:33.840 So call the function we're going to use the name of the function. 00:04:34.150 --> 00:04:34.630 OK. 00:04:34.800 --> 00:04:38.700 Followed by parenthesis. 00:04:39.240 --> 00:04:45.430 We need this time to place the arguments or the parameters. 00:04:46.410 --> 00:04:48.000 So the first vomit cells have 00:04:53.220 --> 00:04:57.120 for example John. 00:04:57.690 --> 00:05:00.820 And the second one for the credits it's a hundred. 00:05:01.650 --> 00:05:09.570 So if we refresh we can see that we have executed the code inside the function. 00:05:09.750 --> 00:05:17.310 Now we can see that we were replaced the the name with the value given to the function and the credits 00:05:18.120 --> 00:05:20.860 with the 100 value as well. 00:05:21.200 --> 00:05:22.410 OK. 00:05:23.430 --> 00:05:25.170 OK. 00:05:27.060 --> 00:05:32.580 Now let's build another example so this time we're going to have a function that will return something 00:05:32.580 --> 00:05:33.650 to us. 00:05:33.650 --> 00:05:34.450 OK. 00:05:35.200 --> 00:05:35.770 So 00:05:38.850 --> 00:05:43.530 we have we're going to need some sort of funk. 00:05:43.570 --> 00:05:46.980 The Viking is just going to return the sum of two numbers. 00:05:47.070 --> 00:05:52.530 So it's going to take a couple of arguments. 00:05:52.530 --> 00:05:55.170 OK so the first one is going to be next. 00:05:55.260 --> 00:06:04.980 And the second one is going to be Y and it's just going to return the sum of x and y. 00:06:05.010 --> 00:06:06.580 All right. 00:06:07.710 --> 00:06:08.860 OK. 00:06:08.880 --> 00:06:09.390 So 00:06:12.120 --> 00:06:16.500 now to call the function we're going to do 00:06:23.010 --> 00:06:24.850 let's just take 00:06:28.500 --> 00:06:37.020 for example a paragraph okay and let's say the sum 00:06:39.540 --> 00:06:43.020 of 7 and 5 00:06:45.600 --> 00:06:48.680 is OK. 00:06:48.810 --> 00:06:50.570 So let's have a variable here. 00:06:50.610 --> 00:06:55.210 So we're going to call some this variable. 00:06:55.210 --> 00:06:56.010 We're going to set it 00:06:58.860 --> 00:07:07.140 to the sum of seven and five. 00:07:09.270 --> 00:07:11.950 Since the function is returning something. 00:07:12.390 --> 00:07:13.020 OK. 00:07:13.020 --> 00:07:19.440 So this value that's the function is going to return to us which is the sum of 4:53 is going to be stored 00:07:19.500 --> 00:07:21.820 inside the some variable. 00:07:22.150 --> 00:07:22.900 Okay. 00:07:23.040 --> 00:07:37.500 So now if we save and refresh we can see that's the some variable sic is storing the sum of 5 and 7 00:07:37.510 --> 00:07:39.550 . 00:07:41.550 --> 00:07:41.730 Okay 00:07:41.760 --> 00:07:49.010 .