Setting parameters and arguments
Video: Setting parameters and argumentsSo I have a simple function here called addTwoNumbers. It just creates two variables, adds them together, and displays the result. And that's fine, but of course this isn't very useful because it's always adding the same two numbers together, always 5 and 10, no matter how many times we call it. Well, I'd like something a bit more flexible than that. I'd like this function to be able to take in some different information to add different numbers together. And I can do that by defining parameters for this function, to say that whenever we call it, we're going to pass information into it.
- Where to go from here
Viewers: in countries Watching now:
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
- Writing source code
- Understanding compiled and interpreted languages
- Requesting input
- Working with numbers, characters, strings, and operators
- Writing conditional code
- Making the code modular
- Writing loops
- Finding patterns in strings
- Working with arrays and collections
- Adopting a programming style
- Reading and writing to various locations
- Managing memory usage
- Learning about other languages
Setting parameters and arguments
So I have a simple function here called addTwoNumbers. It just creates two variables, adds them together, and displays the result. And that's fine, but of course this isn't very useful because it's always adding the same two numbers together, always 5 and 10, no matter how many times we call it. Well, I'd like something a bit more flexible than that. I'd like this function to be able to take in some different information to add different numbers together. And I can do that by defining parameters for this function, to say that whenever we call it, we're going to pass information into it.
That's what the parentheses are for after the function name. They allow us to say do we pass one piece of information into this or two or three or a dozen. And all we need to do here is give it the name of what will become two variables. In this case what I could do is remove my variable declaration here, var a and var b, and just say that this function takes in two parameters called a,b. If I wanted three parameters, I could say a,b,c. I can call them whatever I want, whatever is meaningful.
They obey the same rules as variable names, because really what's happening is we will have variables created for us called a and b. That means the first line that I can run here is var result = a + b. Those parameters that were passed into this function will be added together and will display the result. Now of course the flip side of this is if my function expects information passed into it, well, I better pass information into it when I'm calling it. Now I could create two variables and pass in the variable names or in this case, I could just pass in 5,10. Save that.
When this function is called, 5 will be passed in under the name a. 10 will be passed in under the name b. We save it, we run it, and we should have the 15 alert box being popped in. The flexibility of this simply means that I could call this again, passing in different numbers. Save that, run it again. We'll get three alert boxes, 15, 600, and -2316.
When you start working with defining parameters to your functions, you'll often hear the term arguments as well and parameters and arguments can seem a little bit interchangeable. There is a formal definition. When I'm defining a function, here what I'm defining here are a and b. They're my parameters. When I'm calling the function, I'm passing in 5 and 10. These are my arguments. Now often in conversation with other software developers, these terms are almost interchangeable, but they do have a formal meaning.
Instead of just passing information into a function, I can also pass information back out of a function. The way that we do that is by using a new keyword called return. If I wanted to create say this result by adding these two numbers together and instead of popping up an alert box I just want to pass that result back to whoever called this function, well, what I'm going to say is return result. Now the question is, well, what does that do? Now as soon as you hit the word return, you're actually going to jump right out of this function.
So one of the things you don't want to do is start say creating new variables after this because you'll never actually get to this line of code. When we call the function, we're going to be executing the first line and then the second line and when this one is executed, return result, we're going to jump back to whoever called this. So it's often the last line of the function, although occasionally you can use return to leave a function early if you need to. The question is what happens? If we say return result, well, what's going to happen now? Well, I'm just going to save this and just run this page and we'll see nothing happening at all.
The function is actually being called three times and it's returning results, it's returning these numbers added together. But the problem is I'm not doing anything with those returned results. What I need to do is something like this. Create a new variable and set it equal to whatever happens when I call the addTwoNumbers function. So in this case, we're going to pass in 5 and 10. They will be added together in the function. That number will be returned, and the result will be stored in the variable called x. I could then pop up an alert box, save that over here, and run it.
So both passing information in, passing in arguments to be received as parameters into functions, and returning values is a very core piece of any programming language. It's something you'll be using all the time.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
- Q: Using TextEdit with Mac OS 10.9 Mavericks?
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.