1
00:00:00,810 --> 00:00:05,100
Functions are the basic infrastructure of C plus plus code.

2
00:00:05,100 --> 00:00:08,040
So the code is contained in functions.

3
00:00:08,040 --> 00:00:11,400
To execute that, you have to call a function.

4
00:00:11,400 --> 00:00:17,070
So C plus plus is remarkably flexible in the ways that you can define and call functions here.

5
00:00:17,070 --> 00:00:23,570
So you can define functions with a fixed number or parameters or variable number of parameters.

6
00:00:23,580 --> 00:00:29,220
So you can write generic code so that the same code can be used with different types.

7
00:00:29,220 --> 00:00:33,810
And you can even write generic code with a variable number of types here.

8
00:00:33,810 --> 00:00:37,560
So defining C plus plus functions.

9
00:00:37,560 --> 00:00:41,700
So at the most basic level, a function has parameters here.

10
00:00:41,700 --> 00:00:47,730
So it has the code to manipulate the parameters and it has the return value.

11
00:00:47,730 --> 00:00:52,650
So C plus plus gives you several ways to determine these three aspects.

12
00:00:52,650 --> 00:01:02,170
In this next lecture, we will cover these parts of C plus plus function from left to the right of declaration.

13
00:01:02,170 --> 00:01:08,170
So functions can also be templated, but this will be left to later sections.

14
00:01:08,590 --> 00:01:14,380
So a function must be defined exactly once, but should overloading.

15
00:01:14,380 --> 00:01:20,590
You can have many functions with the same name that differentiate by their parameters.

16
00:01:20,590 --> 00:01:27,580
So code that uses a function has to have access to the name of the function and so it needs to have

17
00:01:27,580 --> 00:01:27,940
access.

18
00:01:27,940 --> 00:01:34,630
So either the function definition or the declaration of the function also called the function prototype.

19
00:01:34,630 --> 00:01:40,690
So the compiler uses the prototype to type check that the calling code is calling the function.

20
00:01:40,690 --> 00:01:42,430
So using the right types here.

21
00:01:42,430 --> 00:01:49,120
So typically libraries are implemented as separate compiled library files and the prototypes of the

22
00:01:49,120 --> 00:01:56,350
library functions are provided in header files so that the many source files can use the functions by

23
00:01:56,350 --> 00:01:57,820
including the heaters.

24
00:01:57,820 --> 00:02:04,690
So however, if you know the function name parameters and return type, you can type the prototype yourself

25
00:02:04,690 --> 00:02:06,250
in your file here.

26
00:02:06,250 --> 00:02:13,210
So whichever you do, you are simply providing the information for the compiler to type check the expression

27
00:02:13,210 --> 00:02:14,470
that calls function.

28
00:02:14,470 --> 00:02:21,130
So it's up to the linker to locate the function in the library and either copy the code into the executable

29
00:02:21,130 --> 00:02:25,630
or set up the infrastructure to use the function from a shared library.

30
00:02:25,630 --> 00:02:32,200
So including the header file from for a library does not mean that you will be able to use the functions

31
00:02:32,200 --> 00:02:39,040
from that library because in standard C plus plus the header file does not have information about the

32
00:02:39,040 --> 00:02:41,620
library that contains a function.

33
00:02:42,080 --> 00:02:45,830
So most of the C runtime library is implemented this way here.

34
00:02:45,830 --> 00:02:53,720
So the function is compiled in a static library or a dynamic link library, and the function prototypes

35
00:02:53,750 --> 00:02:56,210
are provided in a header file.

36
00:02:56,660 --> 00:03:03,140
So you provide a library and the linker command line, and typically you will include the header file

37
00:03:03,140 --> 00:03:08,840
for the library so that the function prototypes are available to the compiler.

38
00:03:08,990 --> 00:03:15,530
So as long as the linker knows about the library, you can type the prototype in your code here.

39
00:03:15,530 --> 00:03:20,940
So this can save you from including some large files into your source files.

40
00:03:20,960 --> 00:03:26,810
The files that will mostly have prototypes of the function that you will not use here.

41
00:03:26,810 --> 00:03:33,440
So far in this course, we have used one source files, so all the functions are defined in the same

42
00:03:33,440 --> 00:03:35,690
file as where they are used.

43
00:03:35,690 --> 00:03:38,480
So and we have defined the function before calling it.

44
00:03:38,480 --> 00:03:44,190
So that is the function is defined above the code that calls it here.

45
00:03:44,190 --> 00:03:44,380
So.

46
00:03:44,430 --> 00:03:48,570
So here you do not have to define the function before it is used.

47
00:03:48,570 --> 00:03:53,220
As long as the function prototype is defined before the function is called here.

48
00:03:53,220 --> 00:03:55,740
So in main function we are going to print something here.

49
00:03:56,010 --> 00:04:01,170
Moved here for example C out mult.

50
00:04:02,280 --> 00:04:04,500
As you can see, we have to input the.

51
00:04:07,120 --> 00:04:07,870
Okay.

52
00:04:08,380 --> 00:04:09,640
Stay out here.

53
00:04:10,510 --> 00:04:16,960
And as you can see, we got an error because we have to import the iostream header file.

54
00:04:17,110 --> 00:04:20,070
And as you can see, we can now print something on the string.

55
00:04:20,080 --> 00:04:22,360
So moved here instead.

56
00:04:23,410 --> 00:04:24,340
And.

57
00:04:25,260 --> 00:04:27,660
Six, six, seven.

58
00:04:29,040 --> 00:04:30,150
And an line.

59
00:04:33,680 --> 00:04:34,190
Here.

60
00:04:34,190 --> 00:04:38,210
And after that, we're going to create the mold function here.

61
00:04:38,360 --> 00:04:40,100
Mold here.

62
00:04:40,460 --> 00:04:43,160
I will show you what's wrong with this code here.

63
00:04:43,870 --> 00:04:45,290
Error is here.

64
00:04:45,290 --> 00:04:50,750
And return the multiply by errors here.

65
00:04:52,100 --> 00:04:57,080
So the mod function here defined after the main function here.

66
00:04:57,080 --> 00:05:00,290
But this code will here.

67
00:05:00,320 --> 00:05:01,910
Let's try this out.

68
00:05:02,840 --> 00:05:04,940
Was actually said here.

69
00:05:10,350 --> 00:05:10,880
Here.

70
00:05:10,890 --> 00:05:18,150
This code will not compile because it is given the prototype before the main function.

71
00:05:18,150 --> 00:05:21,810
So this is called here if we make it like that.

72
00:05:25,330 --> 00:05:31,660
As you can see here, the function can compile and it will multiply six by seven here.

73
00:05:31,690 --> 00:05:36,740
So the prototype doesn't have to be have two parameter names.

74
00:05:36,760 --> 00:05:42,720
So this is because the compiler only needs to know the types of the parameters so their names.

75
00:05:42,730 --> 00:05:48,310
So however, since parameter names should be self-documenting, it's usually a good idea to give the

76
00:05:48,310 --> 00:05:54,370
parameter names so that you can see the purpose of the function here.

77
00:05:55,350 --> 00:06:03,780
Now, here in this lecture, in this example, we the function is defined in the same source file.

78
00:06:03,780 --> 00:06:09,350
So the internal linkage, this means the there is an internal linkage here in this code.

79
00:06:09,360 --> 00:06:15,840
So if the function is defined in another file, the prototype will have external linkage.

80
00:06:15,840 --> 00:06:24,390
And so the prototype will have to be defined, for example, like this here external.

81
00:06:25,830 --> 00:06:28,340
Extend integer mode.

82
00:06:29,200 --> 00:06:29,640
Here.

83
00:06:29,650 --> 00:06:30,520
Integer.

84
00:06:30,550 --> 00:06:31,630
Integer here.

85
00:06:34,350 --> 00:06:41,430
So the extern keyword is one of many specifiers that you can add to a function declaration.

86
00:06:41,430 --> 00:06:45,950
And in the previous lectures we have seen others.

87
00:06:45,960 --> 00:06:51,780
So for example, the static specifier can be used on a prototype to indicate that the function has internal

88
00:06:51,780 --> 00:06:56,760
linkage and the name can only be used in the current source file.

89
00:06:57,330 --> 00:07:05,280
So current source file in this example here in for example, static.

90
00:07:06,710 --> 00:07:07,340
Integer.

91
00:07:08,140 --> 00:07:11,530
So you have to write static before the function return type here.

92
00:07:11,740 --> 00:07:13,450
So in this example.

93
00:07:14,640 --> 00:07:18,730
It is appropriate to mark the function as static.

94
00:07:18,750 --> 00:07:19,530
In this case.

95
00:07:19,530 --> 00:07:27,390
So you can also declare a function as external here which affects how the name of the function is stored

96
00:07:27,390 --> 00:07:30,420
in the object file.

97
00:07:30,420 --> 00:07:35,370
So this is important for libraries and will be covered shortly.
