1 00:00:07,010 --> 00:00:09,640 Functions are sections of code used within a larger program 2 00:00:09,640 --> 00:00:11,430 to perform a particular task. 3 00:00:11,430 --> 00:00:15,060 They are also known as procedures, subroutines, and subprograms, 4 00:00:15,060 --> 00:00:18,320 since they really are just mini programs within programs. 5 00:00:18,320 --> 00:00:20,340 Even though you might not have noticed, 6 00:00:20,340 --> 00:00:24,090 you most definitely have used functions already if you've programmed in C. 7 00:00:24,090 --> 00:00:26,770 When you write stuff to the screen using printf, 8 00:00:26,770 --> 00:00:29,380 you're actually using a function called printf. 9 00:00:29,380 --> 00:00:33,760 Likewise, GetInt and GetString are functions that CS50 provides for you. 10 00:00:33,760 --> 00:00:37,750 As programmers, we use functions for a number of reasons. 11 00:00:37,750 --> 00:00:41,240 >> Having functions to perform common tasks like writing to the screen 12 00:00:41,240 --> 00:00:44,480 and prompting the user for input saves us time when programming. 13 00:00:44,480 --> 00:00:47,510 It's not necessary to write code to do those same things 14 00:00:47,510 --> 00:00:49,520 since they're already done for us. 15 00:00:49,520 --> 00:00:54,580 It's not even necessary for us to understand exactly how those functions work internally. 16 00:00:54,580 --> 00:00:58,880 Instead, we just have to know how to use or call them in our programs. 17 00:00:58,880 --> 00:01:02,040 It's also helpful to write functions in larger programs 18 00:01:02,040 --> 00:01:04,330 even if you're not planning on having others use them, 19 00:01:04,330 --> 00:01:06,830 just to divide up the larger task at hand. 20 00:01:06,830 --> 00:01:11,910 This way, you give structure to your code much like you'd give structure to an essay, 21 00:01:11,910 --> 00:01:15,180 and with really large projects you enable multiple people 22 00:01:15,180 --> 00:01:16,730 to work on different parts at the same time 23 00:01:16,730 --> 00:01:20,480 since it's clear where one person's work ends and another's begins. 24 00:01:20,480 --> 00:01:26,530 >> Functions typically operate on 1 or more inputs, known as arguments or parameters, 25 00:01:26,530 --> 00:01:30,520 transforming those inputs into an output value that is then returned. 26 00:01:30,520 --> 00:01:34,950 However, it's also possible to have functions that don't use any input arguments 27 00:01:34,950 --> 00:01:36,400 and don't return an output. 28 00:01:36,400 --> 00:01:38,790 Okay, enough with the fluffy stuff. 29 00:01:38,790 --> 00:01:41,790 Let's take a look at some real functions in C. 30 00:01:41,790 --> 00:01:45,570 >> Here we have a function called square that, believe it or not, 31 00:01:45,570 --> 00:01:47,570 computes the square of the input. 32 00:01:47,570 --> 00:01:49,490 Let's dissect the syntax of it. 33 00:01:49,490 --> 00:01:51,860 The first word you see in the function, int, 34 00:01:51,860 --> 00:01:57,460 specifies the return type of the function; that is, the C type of the function's output. 35 00:01:57,460 --> 00:02:00,370 This function will output a value with type int. 36 00:02:00,370 --> 00:02:04,100 The next word you see is the name of the function, square in this case. 37 00:02:04,100 --> 00:02:07,390 Naming functions appropriately is particularly important 38 00:02:07,390 --> 00:02:11,260 since it's probably the most visible documentation of what the function does. 39 00:02:11,260 --> 00:02:14,880 It might be cool to name this function shazam or booyah, 40 00:02:14,880 --> 00:02:18,340 but that wouldn't tell us anything about what the function does. 41 00:02:18,340 --> 00:02:23,470 >> Following the function name come the inputs to the function, the arguments. 42 00:02:23,470 --> 00:02:26,380 They are listed inside a set of parentheses 43 00:02:26,380 --> 00:02:30,160 with both their type and a name to use for them within the body of the function. 44 00:02:30,160 --> 00:02:34,030 Square takes just 1 argument, the int to square, 45 00:02:34,030 --> 00:02:36,160 which I've decided to give the name x. 46 00:02:36,160 --> 00:02:40,760 This ends the function declaration or prototype. 47 00:02:40,760 --> 00:02:43,320 The curly braces surrounding the rest of the function 48 00:02:43,320 --> 00:02:45,870 denote the beginning and end of the function body, 49 00:02:45,870 --> 00:02:48,320 the definition of the function itself. 50 00:02:48,320 --> 00:02:50,300 Square is a pretty simple function. 51 00:02:50,300 --> 00:02:54,100 It's only got 1 line, since all we have to do is multiply x by itself. 52 00:02:54,100 --> 00:02:59,250 The word return specifies the output value and triggers the end of the function. 53 00:02:59,250 --> 00:03:03,430 To use the square function, we need to supply the appropriate inputs 54 00:03:03,430 --> 00:03:05,310 and then capture the output. 55 00:03:05,310 --> 00:03:08,140 Here, you see a couple of ways of doing that. 56 00:03:08,140 --> 00:03:13,600 We can pass in a raw int, like 6, or we can pass in a variable like a. 57 00:03:13,600 --> 00:03:15,700 Note that each time we call square, 58 00:03:15,700 --> 00:03:18,330 we capture the output in a variable and then print it. 59 00:03:18,330 --> 00:03:22,690 However, we don't have to store the return value in a variable and then print. 60 00:03:22,690 --> 00:03:27,090 We could just send it directly to printf as we do when we square 10, 61 00:03:27,090 --> 00:03:30,580 but this does mean that we don't have access to the return value of square 10 62 00:03:30,580 --> 00:03:32,230 elsewhere in the code. 63 00:03:32,230 --> 00:03:34,890 Let's look at another simple example. 64 00:03:34,890 --> 00:03:38,750 >> This time, we'll sum 2 floats together so that we can see what a function looks like 65 00:03:38,750 --> 00:03:41,220 when it's got 2 inputs instead of just 1. 66 00:03:41,220 --> 00:03:43,950 As you can see, it's not much different. 67 00:03:43,950 --> 00:03:47,330 All we do is add in the 2nd argument, a float named b, 68 00:03:47,330 --> 00:03:51,820 to the argument list using a comma to separate it from the first argument, float a. 69 00:03:51,820 --> 00:03:55,550 So, our square and sum functions are pretty easy to understand 70 00:03:55,550 --> 00:03:58,930 because they're equivalent to functions that you've seen before in math class. 71 00:03:58,930 --> 00:04:01,610 Now, let's look at a function that is slightly different. 72 00:04:01,610 --> 00:04:04,620 Instead of returning a value, it modifies a state. 73 00:04:04,620 --> 00:04:07,260 This is known as having a side effect. 74 00:04:07,260 --> 00:04:10,750 In this program, the printf_fudd_style function 75 00:04:10,750 --> 00:04:13,410 has a return type that we haven't seen before, void. 76 00:04:13,410 --> 00:04:16,730 >> Void is used to say that a function doesn't return anything. 77 00:04:16,730 --> 00:04:19,410 There are no variables of type void, 78 00:04:19,410 --> 00:04:22,760 so if you try to return something from a function like printf_fudd_style, 79 00:04:22,760 --> 00:04:24,290 the compiler will yell at you. 80 00:04:24,290 --> 00:04:29,390 Inside printf_fudd_style, we see that while the function still performs a task, 81 00:04:29,390 --> 00:04:31,890 namely, switching all Rs to Ws, 82 00:04:31,890 --> 00:04:36,380 the point of the function is to modify the string and then print it out on the screen, 83 00:04:36,380 --> 00:04:39,400 changing the state of the program instead of returning a result. 84 00:04:39,400 --> 00:04:43,700 Using this function is very much like using our square and sum functions, 85 00:04:43,700 --> 00:04:46,950 except we don't store the result in a variable or pass it off to another function 86 00:04:46,950 --> 00:04:49,520 since there is no result to speak of. 87 00:04:49,520 --> 00:04:53,180 So, when we compile and run our Elmer Fudd program, 88 00:04:53,180 --> 00:04:56,970 you see that "You rascally rabbit!" gets transformed right to 89 00:04:56,970 --> 00:04:58,730 "You wascally wabbit!" 90 00:04:58,730 --> 00:05:02,250 >> Finally, there's 1 more function worth discussing 91 00:05:02,250 --> 00:05:06,810 since you've been using it in every C program you've ever written, main. 92 00:05:06,810 --> 00:05:09,450 Main is a function much like any other. 93 00:05:09,450 --> 00:05:13,580 It's got a return type, a name, a list of arguments, and a body. 94 00:05:13,580 --> 00:05:16,110 The return type of main is always an int, 95 00:05:16,110 --> 00:05:19,120 and this int is used to communicate the status of the program when it finishes. 96 00:05:19,120 --> 00:05:23,360 >> Did it run successfully, or did it end early because of an error? 97 00:05:23,360 --> 00:05:26,390 Returning to zero indicates a successful run, 98 00:05:26,390 --> 00:05:29,510 and returning any non-zero value indicates an error. 99 00:05:29,510 --> 00:05:31,950 Depending on the kind of program you're writing, 100 00:05:31,950 --> 00:05:34,960 returning different codes for different errors can be helpful 101 00:05:34,960 --> 00:05:37,210 for people who use your program later on. 102 00:05:37,210 --> 00:05:40,220 Mains arguments are a little more complicated. 103 00:05:40,220 --> 00:05:42,150 >> The argument list can either be empty, 104 00:05:42,150 --> 00:05:45,330 which we indicate by writing void in between the parentheses, 105 00:05:45,330 --> 00:05:50,220 or the list can have 2 arguments: an int and an array of char*s. 106 00:05:50,220 --> 00:05:52,820 These are used when your program intends to use 107 00:05:52,820 --> 00:05:56,490 arguments passed in on the command line when the program is invoked. 108 00:05:56,490 --> 00:05:59,690 The int argument is typically called argc, 109 00:05:59,690 --> 00:06:03,300 and it's equal the length of the char* array, known as argv. 110 00:06:03,300 --> 00:06:07,080 Argv contains the command line arguments passed into the program. 111 00:06:07,080 --> 00:06:11,440 In another short, we'll talk about how to use these variables in more detail. 112 00:06:11,440 --> 00:06:15,220 For now, you can feel free to declare main either way. 113 00:06:15,220 --> 00:06:19,410 I typically choose to write it out the shorter way, with no arguments, when possible. 114 00:06:19,410 --> 00:06:22,230 >> Now that you know how to write functions, 115 00:06:22,230 --> 00:06:25,030 you're well on your way to developing larger, more complex programs. 116 00:06:25,030 --> 00:06:29,740 My name is Nate Hardison. This is CS50.