6.2 Understand the basics of function arguments - Video Tutorials & Practice Problems
Video duration:
10m
Play a video:
Video transcript
<v Voiceover>One of the</v> key parts of functions is that they take in arguments. These arguments are input to the function that help control what it does. But before we dive into that, we're going to learn about a new function called sprintf, just to see how it works, because we're going to use that extensively as we explain arguments. Though sprintf is a function that takes, as its first value, some string. First I'll be, hello %s, close the strain. What that means is it'll print hello, and then wherever it sees %s, it'll sub in the next argument. In this case the next argument will be Jared. So when we run this it will say, hello Jared. There we have it. Now sprintf can take on multiple %s symbols. So we're going to do sprintf, hello, %s, comma, today is %s. Close the quotes. We'll make the first argument Jared, I'm sorry that's the second argument we're making, now it's the first after the string. And the third argument will be Sunday. Now it's gonna print out, hello Jared, today is Sunday. Let's copy and paste that. We can make this Saturday, and now it's gonna print out Saturday. So sprintf is a nice substitution function for building up strings. Let's clear this out of the way using control L, and let's build a new function. We will call this function hello.person, and we assign to that variable a function that takes one argument called name. We build our opening and closing braces to keep things neat, and we're gonna say print what we get from sprintf, in sprintf we're gonna feed it hello %s, and that %s will take on whatever value name has. So we close off sprintf and we close off print. We get the function ready to be used by highlighting it all and running it. And now we can call it a function. So for instance, hello.person, and we'll put in Jared, and we see it says hello Jared. We can do hello.person, Bob, and it says hello Bob. And, as one more test we can do hello.person, Sarah. And it now says hello Sarah. So, that is just one argument as an input to a function. It's possible in r to have an arbitrary number of arguments. So, let's clear out the console and build a new function. This function we will also call hello.person, so it will overwrite the old function, and it'll take two arguments, first and last. We set up our curly braces, and in here we put print, because we want to print out the result, sprintf is how we build the string, and we will have it say hello %s space %s. And the replacements for those %s's will be first and last. Close the parenthesis for sprintf and close it for print. Before we run the function we should look at what hello.person looks like right now. It's still the old hello.person. Now, if we run the function, we get an error. We built this function, we just typed it out. So, it's trying to call hello.person as it currently exists. What we have to do is assign that to a function. Now that we've done that, we can run the whole function definition, and then call hello.person, and it shows the new function that we wanted. Now, we can start using it. Do hello.person, then we say Jared comma Lander. And it says Hello Jared Lander. Doing it this way, it is using the arguments positionally. It assumes the first argument goes with first and the second argument goes with last. We could, however, do this, hello.person, last equals Lander and first equals Jared. Since we've specified the argument names, it doesn't matter which one comes first, it still prints it out correctly, Hello Jared. We could, if we have to, specify one argument but not the other one. So we have Jared, and we say last equals Lander. Now in this case it doesn't really matter because there's only two arguments, but that's useful, being able to name some and not the other. This would be particularly important if we did it in the other order. Hello.person, last equals Lander, and then just set Jared without an argument name. Since there are only two arguments, r is smart enough to figure out that Jared goes to the remaining argument. Arguments are how you get information into functions, and you can define as many arguments as you need to make the function work. In this case, we only ever use two, and it sufficed for our needs. As functions become more complex, you might need more. With all these arguments, sometimes it's handy to not have to specify them all and still have the function work. This is where default arguments come in. When writing the function, you can specify if a certain argument doesn't have a value provided, use a default one. So, let's clear out the console and take a look. Let's build out another hello.person function. The first argument will still be first, and the second argument will be last, except this time we'll put in Doe as a default argument. So if no last is provided, Doe will be put there. And here we're gonna put again print, sprintf, hello %s, space, %s, and the additional arguments to sprintf are first and last. Running this function puts it into memory. Now we can go ahead and call hello.person, and we'll use Jared and Lander, and it works as it did before. Now however, if we do hello.person and we just say Jared, it says Hello Jared Doe, because it picked up on the default argument for last. That could be very very useful to use. If we were to just specify the last argument, last equals Lander, this will not work, because we've specified last but not first and first did not have a default argument. So, default arguments only work if those are the arguments that weren't provided. Let's run this one more time, just to see it in action. We will say first equals Jared, that's all we will give it, and it provides Jared Doe. Let's clear out the console again. Being that you can specify arguments by name, what happens if you specify it by the wrong name? So let's call hello.person, we'll say Jared, and we'll say extra equals Goodbye. When we run this, we get an error because there is no argument called extra. Now, even if we were to call hello.person, not provide names but use three arguments, we will once again get an error because there is no room for three arguments. So it's very important that you use the correct number of arguments and the correct names of arguments if you are using names. One way to get around this is through the ... argument. Now, the ... argument is very tricky, and actually is very very powerful when used correctly. Right now we are going to underutilize it by just using it as a catch-all to catch all extraneous objects. This is well beneath it's stature, it can do so much more, but it'll help us perfectly with what we want right now. So let's clear out the console, do hello.person yet again, and this time we will define it with a first argument, a second argument that has a default value, and ... That ... will catch any extra things we put in. Again, we're going to print a sprintf that says hello %s %s, and there we put first and last. Running this gets it ready, and now we can call hello.person Jared, Lander, Goodbye. And goodbye's completely ignored. We can even do hello.person, Jared, and we'll just say, extra equals goodbye. And here, Jared was used for the first, Doe was used as the default for last, and extra equals goodbye was just ignored. This ... is really, really, helpful. Arguments of many kinds play a vital role of writing functions. They allow you to specify inputs to the function and control what's going to happen. You can have any number of arguments. If the user specifies them by name, they can go in any order, and you can have catch-alls for extra arguments. Being able to properly build a function with the correct arguments is a key part of our programming, and is an essential skill to learn.