02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous Video
Chapter is concluded.Next is Objects.

Making Default Paramters

Some times you want to have functions that by default would take one value as long as no other value was sent to them. In this video we will look how to set a default value in AS3 functions.

Video Transcript

00:00One last thing I wanted to touch upon and I already started writing the word function
00:04I'm just gonna create a quick function which will it be a function that says hello
00:10Hello user it would accept one parameter which be which it would be user and that would be a string
00:16It wouldn't return anything and all it would do is trace out
00:22Hello plus the username thus if I call my hello
00:30User and send to my name
00:33Then we should get in our output. Hello user. Well, hello Ben actually, right?
00:39Now what would happen if we would call hello you hello user and not send it any information
00:45If we wouldn't send any information we would get an error because it would tell us that there's account mismatch
00:51that it was expecting one and it got zero and I'm ignoring all those little details because
00:55The method info one completely ignored reason is because this function is not really the main function. This is our main function
01:03So flash is telling us that method the first method inside of main
01:09Generated an error which is kind of a really weird error
01:12But we know though that something is wrong inside of our main
01:16the next thing that we want to do is we want to change this or
01:20We want to add the possibility to make the user not a mandatory parameter and the way we do that we could send a
01:28Value just adding equal like we did our variables and we could set a default value such as Ben
01:35By doing this now Ben is the default value thus if I call a low user and send it Ben
01:41It would output Ben. Hello Ben. And if I would send it any other information such as
01:47Not been it would output not been so basically it puts here a default
01:55value
01:56Now let's see another up another option here now. I'm just gonna say hello you understand take a low user
02:02I'm gonna duplicate it call it a low user to hello
02:08two users
02:09Let's say that user two is not a mandatory
02:14First user is mandatory now the way we do that always the mandatory items have to come first
02:20If we have any non mandatory items any items that we are, you know putting some default values
02:26They have to be the last on the list
02:28So first on the list have to be the ones that are mandatory the ones that we don't put any equal values to them
02:33and only after that we could put items that are
02:37optional and
02:38You'll see as you start playing with functions and working with functions
02:41You're gonna see that over and over that there are a lot of methods
02:44That have a lot of properties
02:46But you don't have to fill them because they're default values are the ones that are gonna you're gonna normally want to use
02:52But let's see how we how we could work with this. So hello user two
03:00said
03:02Which is obviously random, right?
03:06You sir now if I call the function now and again if I call our function hello user
03:13user
03:15Hello user. Hello to user and I don't send anything to it. It's gonna generate an error, right? Oh
03:22We're having a different error
03:24And what is it telling us?
03:27Implicit correction of value type string to an unrelated type number. Where do we have here a number?
03:34Very weird
03:37Let's instead of trying to solve this let's just put commas instead of pluses. Oh
03:43You see I put here two pluses. There you go. So let's undo that and let's delete that extra plus
03:53Here we go. So now we're getting the error that we were expecting
03:56It's again telling us that method our second method inside of you know
03:59It's kind of random this name because they can't really figure out what's the name of the function. It's kind of a
04:05The flash debugger isn't that detailed but it's telling us that there's some sort of mismatch and we know that our mismatch is happening
04:11Right here. So if I say hello user
04:14Jerry
04:15And I just put one parameter the second one will get filled by default because I don't have to put the second one because it's not
04:21A mandatory one. So if I run the app again, we're gonna see
04:25Hello, Ben said Jerry. We probably want to put here a space
04:30Okay, now if we wanted to we could have also overridden that value right we didn't have to use Ben we could have used
04:36Tom and
04:39It would change it around
04:41Okay, so this is the non mandatory parameters
04:45They were introduced in ActionScript 3 they didn't exist previously, which is kind of cool
04:50And as to everything wasn't really mandatory you could have sent as many parameters as you wanted
04:55You didn't have to define it though in AS 3 you kind of need to define it
05:00Well in non strict mode you could call a function. For example, we could have called hello user 2 and add 3 parameters
05:07That's not true in strict mode
05:09And that's why is for example a third parameter that we're not really using if we would call this function
05:14We're gonna get an error because we're running in strict mode and we really don't want to play around and change those
05:19configurations because that's what most developers are expecting and
05:24Basically, that's the direction that
05:26Adobe wants to go

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!