1 00:00:00,240 --> 00:00:01,350 Welcome back. 2 00:00:01,350 --> 00:00:03,740 Now that we've discussed returning values from functions, 3 00:00:03,740 --> 00:00:07,540 we're going to build on this by learning how to send values to functions. 4 00:00:07,540 --> 00:00:09,980 We've been using some pretty simple examples so far. 5 00:00:09,980 --> 00:00:13,470 Our function that adds two plus two together wasn't very interesting, 6 00:00:13,470 --> 00:00:15,770 even after we returned a value from it. 7 00:00:15,770 --> 00:00:19,510 You might be thinking I still don't get why functions are so useful. 8 00:00:19,510 --> 00:00:21,690 Well, let's expand on this. 9 00:00:21,690 --> 00:00:24,390 Our 2 plus 2 function would be a lot more useful 10 00:00:24,390 --> 00:00:29,930 if we could use it to add the number 2 to any integer and we can totally do that. 11 00:00:29,930 --> 00:00:34,280 Just like functions can send values back via a return, functions can receive 12 00:00:34,280 --> 00:00:39,580 values too and they can see, access and use those values inside the function. 13 00:00:39,580 --> 00:00:43,617 This is all done by what’s called passing an argument to a function. 14 00:00:43,617 --> 00:00:45,280 There are two parts to this. 15 00:00:45,280 --> 00:00:48,590 First, when we call a function, we can send a value. 16 00:00:48,590 --> 00:00:51,910 Second, the function has to be written so we can receive that value. 17 00:00:53,160 --> 00:00:55,890 Have you ever wondered about those pesky parenthesis we have to 18 00:00:55,890 --> 00:00:58,440 add when defining or calling the function? 19 00:00:58,440 --> 00:01:02,860 Those parens are where the sending and receiving of values takes place. 20 00:01:02,860 --> 00:01:05,570 Let's revisit our two plus two function. 21 00:01:05,570 --> 00:01:07,820 Okay, first, I'm gonna change its name. 22 00:01:07,820 --> 00:01:11,990 Instead of 2 + 2, I'll call it add_two. 23 00:01:13,412 --> 00:01:17,112 This new name makes more sense because we're gonna modify this function so 24 00:01:17,112 --> 00:01:20,300 it now adds 2 to whatever integer it receives. 25 00:01:20,300 --> 00:01:23,250 Now inside the parens, I'm gonna create a variable. 26 00:01:23,250 --> 00:01:24,320 I'm gonna call it num. 27 00:01:25,760 --> 00:01:29,510 Right now num has no value, it won't be assigned a value until the function is 28 00:01:29,510 --> 00:01:33,890 called and a value is passed but we'll get to that in just one moment. 29 00:01:33,890 --> 00:01:36,340 Now that we've created a variable inside the prints, 30 00:01:36,340 --> 00:01:39,760 this special kinda of variable is known as a parameter by the way. 31 00:01:39,760 --> 00:01:43,720 We can use it inside of our function just like any other local variable. 32 00:01:43,720 --> 00:01:46,960 That means we can change the body of our function like this. 33 00:01:50,200 --> 00:01:53,780 What we've done here is rewrite our function so that it can receive a value, 34 00:01:53,780 --> 00:01:57,090 which is stored in the parameter variable called num. 35 00:01:57,090 --> 00:01:59,450 And then we can use that value inside the function. 36 00:02:00,740 --> 00:02:03,250 So how do we send that value to our function? 37 00:02:03,250 --> 00:02:04,670 Well, this happens when we call it. 38 00:02:04,670 --> 00:02:12,690 First we'll rate a regular function call to the add_two function. 39 00:02:12,690 --> 00:02:15,950 To send a value, we add it inside the parens. 40 00:02:15,950 --> 00:02:17,940 This value is called an argument. 41 00:02:17,940 --> 00:02:22,570 An argument can be any Python data type or expression that evaluates to a value. 42 00:02:22,570 --> 00:02:26,490 This means you can pass in an integer, a string, a function call, and 43 00:02:26,490 --> 00:02:29,640 even other Python data types that you haven't learned about quite yet. 44 00:02:29,640 --> 00:02:32,190 In our case, we're just gonna be passing an integer. 45 00:02:32,190 --> 00:02:33,064 Let's pass the number 5. 46 00:02:35,463 --> 00:02:37,530 Now, like we learned earlier in this course, 47 00:02:37,530 --> 00:02:40,920 not much will be visible to us if we run our program right now. 48 00:02:40,920 --> 00:02:44,475 To add some clarity to what's going on behind the scenes, I'll add a print 49 00:02:44,475 --> 00:02:48,440 statement inside our function that prints out the value sent to the num parameter. 50 00:02:53,963 --> 00:02:58,088 Now when I run this we can expect the output to be five, let's double check. 51 00:03:05,683 --> 00:03:09,851 Awesome, just as I expected the output here is 5 because when we called 52 00:03:09,851 --> 00:03:15,090 the add_two function we passed the integer 5 as an argument to the function. 53 00:03:15,090 --> 00:03:18,905 This value was received by our function and stored in the variable num. 54 00:03:18,905 --> 00:03:21,711 Num, now referencing a value of five, 55 00:03:21,711 --> 00:03:25,798 can be used like any local variable inside the function. 56 00:03:25,798 --> 00:03:27,692 I hope you're getting the hang of arguments and 57 00:03:27,692 --> 00:03:29,018 parameters when you're ready, 58 00:03:29,018 --> 00:03:32,650 come join me in the next video to talk about handling more than one parameter. 59 00:03:32,650 --> 00:03:35,950 Also don't forget that you're doing great and you're learning things that are going 60 00:03:35,950 --> 00:03:39,460 to help you be more experienced and capable programmer. 61 00:03:39,460 --> 00:03:41,240 If you're struggling, it's okay. 62 00:03:41,240 --> 00:03:44,030 Treehouse has tons of resources to help you support you while you go through 63 00:03:44,030 --> 00:03:45,440 your coursework. 64 00:03:45,440 --> 00:03:48,940 The Treehouse community is an excellent place to connect with other students and 65 00:03:48,940 --> 00:03:50,890 ask and answer questions. 66 00:03:50,890 --> 00:03:54,520 Watching videos a second time can also help you absorb information. 67 00:03:54,520 --> 00:03:56,470 Sometimes you really need to hear things two or 68 00:03:56,470 --> 00:03:59,740 three times before a concept really starts to sink in. 69 00:03:59,740 --> 00:04:02,960 Think about a time when you've watched a movie a second or third time. 70 00:04:02,960 --> 00:04:06,950 How much more did you pick up or notice, learning programming is a lot like that. 71 00:04:06,950 --> 00:04:10,940 And finally don't hesitate to practice, there is no wrong way to practice or 72 00:04:10,940 --> 00:04:13,790 learn and trying things out in your terminal is a great way to 73 00:04:13,790 --> 00:04:18,510 understand what's happening in your code without worrying about breaking anything. 74 00:04:18,510 --> 00:04:21,610 Taking advantage of Treehouse's practice sessions is another way to gain 75 00:04:21,610 --> 00:04:24,180 a real advantage in your skill development. 76 00:04:24,180 --> 00:04:28,150 Remember, just have fun and always keep in mind that even professional and 77 00:04:28,150 --> 00:04:30,890 advanced programmers are always learning and getting better too.