1
00:00:00,570 --> 00:00:01,790
Hello and welcome.

2
00:00:01,900 --> 00:00:08,230
In this video I will be introducing you to javascript functions.

3
00:00:08,550 --> 00:00:18,480
Functions are a way of packaging functionality that you wish to reuse.

4
00:00:19,260 --> 00:00:33,000
So a function basically can refer to a block or a piece of code that is designed to perform a particular

5
00:00:33,420 --> 00:00:35,590
task.

6
00:00:35,790 --> 00:00:44,540
Jemas group function is execute it when some theme in it back evil a mean cause it.

7
00:00:44,700 --> 00:00:49,360
So in order to activate a function after it has been created.

8
00:00:49,400 --> 00:00:52,510
We need to invoke it or call it.

9
00:00:52,560 --> 00:00:56,720
And usually you do that by its name.

10
00:00:57,100 --> 00:01:09,090
Javascript has many built in functions that you can use very low on his alert which enables you to create

11
00:01:09,160 --> 00:01:11,860
pop up box messages.

12
00:01:12,000 --> 00:01:20,670
Javascript also enables you to create your own functions and the way you create your functions in Javascript

13
00:01:20,970 --> 00:01:33,320
is by using the function key word from chao's often takes arguments and arguments are bits of data that

14
00:01:33,320 --> 00:01:38,440
the functions need in order to perform or do their job.

15
00:01:38,580 --> 00:01:46,790
And these arguments are placed inside parentheses which are separated by commas.

16
00:01:46,820 --> 00:01:50,070
If there are more than one argument.

17
00:01:50,640 --> 00:01:59,520
So inside my console I have created a function and the way you create a function you Busy B have to

18
00:01:59,520 --> 00:02:04,290
type in the keyword function followed by the name you want to call the function.

19
00:02:04,290 --> 00:02:13,710
When you create creating function it's very nice to name your functions as close as possible to the

20
00:02:13,710 --> 00:02:17,100
task that the function is meant to perform.

21
00:02:17,460 --> 00:02:24,600
See my function I've called it and none which indicates that this function is going to be used to add

22
00:02:24,690 --> 00:02:25,910
numbers together.

23
00:02:26,070 --> 00:02:32,940
So when you create a function you have to type in the word function followed by this space then the

24
00:02:32,940 --> 00:02:36,120
name of the function and then you have to have parent.

25
00:02:36,120 --> 00:02:43,460
This is sometimes the parentheses can be empty but you must have that in order to create a function.

26
00:02:43,740 --> 00:02:48,730
And then if you've got any arguments you parse the argument inside the parentheses.

27
00:02:48,730 --> 00:02:52,890
If there's more than one argument you separate them by a comma.

28
00:02:52,890 --> 00:02:54,690
You can see here number one.

29
00:02:54,780 --> 00:02:58,730
Here's an argument not to refer to as an argument.

30
00:02:58,980 --> 00:03:05,970
And once you specify the arguments if there are any inside the parentheses you have this curly braces

31
00:03:05,970 --> 00:03:13,380
you call the opening curly braces here and the closing curly braces and inside in between the curly

32
00:03:13,380 --> 00:03:23,400
braces is the block of code that has to be executed when the function is invoked or called.

33
00:03:23,400 --> 00:03:29,260
Notice here also caused a return mistake here.

34
00:03:29,490 --> 00:03:38,880
Basically the return statement tells the browser to return the result variable out of the function so

35
00:03:38,880 --> 00:03:41,790
it is available to use right.

36
00:03:41,820 --> 00:03:51,510
This is necessary because variables defined inside functions are only available inside does function.

37
00:03:51,510 --> 00:04:00,480
This is called variable scope so when you create variables variables can be available try out the application

38
00:04:00,870 --> 00:04:09,870
or out the scope which is known as global so if you create a variable and you declare it outside the

39
00:04:09,870 --> 00:04:16,610
function that makes that variable global That means you can use it anywhere within the block of code.

40
00:04:16,820 --> 00:04:24,430
If you define a variable inside a function like I done here I've defended variable called result and

41
00:04:24,420 --> 00:04:32,460
I've set that variable result to cause to whatever value is passed as number one and add added to value

42
00:04:32,460 --> 00:04:35,120
passed as number two.

43
00:04:35,400 --> 00:04:45,250
So this variable is local to this function so he can only be accessed inside this function so when a

44
00:04:45,280 --> 00:04:54,440
variable exists inside a function as a function has been executed variable basically is destroyed.

45
00:04:54,450 --> 00:04:59,820
Or you know it's because on you you can't use it again.

46
00:05:00,060 --> 00:05:01,110
China has been called that.

47
00:05:01,110 --> 00:05:02,930
It is served its purpose.

48
00:05:03,000 --> 00:05:11,430
However if you want the variable to be available you have to use the written statement that way to tell

49
00:05:11,430 --> 00:05:21,900
the browser to return the result or the variable out of the function so that it is available to use.

50
00:05:21,960 --> 00:05:24,110
You'll only do this for variables.

51
00:05:24,110 --> 00:05:33,150
This site defined inside the function then known as a local variable scope so variables defined outside

52
00:05:33,150 --> 00:05:34,690
of function global.

53
00:05:34,890 --> 00:05:42,510
So you can access them from anywhere within the US within the javascript code whereas variables defined

54
00:05:42,750 --> 00:05:50,350
inside function like I have done here referred to as a local scope variable.

55
00:05:50,400 --> 00:05:53,870
So here I have defined this variable at the nump.

56
00:05:53,970 --> 00:06:00,310
Now to make or to invoke this function not I have created it.

57
00:06:00,330 --> 00:06:13,080
All I need to do is call the function his name which is add on and then I have to you must add the parentheses

58
00:06:13,570 --> 00:06:18,410
and then I'm going to pass it to values because I've got two arguments there.

59
00:06:18,560 --> 00:06:31,700
So if I see 10 comm. are 20 and I close the parentheses and the press Enter now add 10 and 20 together

60
00:06:31,940 --> 00:06:34,740
and give me 30.

61
00:06:34,750 --> 00:06:40,050
All right so any time I want to use this function all I need to do is call the function by his name

62
00:06:40,650 --> 00:06:48,220
and then pass in two arguments so you can see he sees me having to rewrite the function again.

63
00:06:48,240 --> 00:06:50,900
I've already written it to find it.

64
00:06:51,090 --> 00:06:57,920
All I need to do is just call it parsing whatever arguments I need for the functions.

65
00:06:57,960 --> 00:07:02,460
For example you can create the same page maybe on what application or anything.

66
00:07:02,460 --> 00:07:12,000
This is just a basic illustration to show you the importance of functions so you can create functions

67
00:07:12,000 --> 00:07:15,040
for different kinds of task or to do different things.

68
00:07:15,060 --> 00:07:20,550
But once you've created the function you then have to write the code again or you need to do is just

69
00:07:20,640 --> 00:07:25,240
call the function by its name parsee whatever arguments you need to do.

70
00:07:25,250 --> 00:07:29,500
Mike I've passed in here and been at the function execute.

71
00:07:29,630 --> 00:07:44,860
If I want to pass in more argument I just call the function is the name that passes in C at 6 and 6.

72
00:07:45,010 --> 00:07:46,630
Now add six the six together.

73
00:07:46,650 --> 00:07:52,870
It will return 12 because I have already done it in all the functions here.

74
00:07:53,070 --> 00:08:00,090
So all I need to do is call it base name and pass him arguments so that's basically how you create and

75
00:08:00,090 --> 00:08:05,670
use function functions are a very powerful concept in javascript.

76
00:08:06,010 --> 00:08:10,070
I'm only barely scratched the surface here.

77
00:08:10,200 --> 00:08:14,870
This is just an introduction to Javascript function.

78
00:08:14,880 --> 00:08:16,920
There's a lot more to it.

79
00:08:17,220 --> 00:08:18,960
So that's it for this too.

80
00:08:18,960 --> 00:08:21,280
Thank you for watching and bye for now.
