I welcome back to Mom's a Force equal programming to we're in the last subsection of lesson one sequel functions. This is we're gonna finish up our name porous function.
In the previous lesson, we parsed out the last name and this lesson we're going to integrate the pursing of the first name from the string. Now, a couple things to keep in mind with this function is that this function works specifically
against a certain format. So if the format of the name changed, of course, it would not produce the correct results. For example, if for some reason somebody passed in a name that was
last name Dash first name, it would not function correctly because it's looking for commas
and not dashes. So that's something to keep in mind when working with this type of functions that you've developed to deal with a specific problem, because occasionally you'll run into instances where somebody has decided
to change the ah, the pattern and then your functions that would chase that pattern no longer work correctly. With that said, let's go ahead and get started and finished this function up.
So if we look back at the query, we had,
ah couple things we had to do to get the first name.
Um, first we would get the sub string.
Then we would send to the ah layer below.
And then we also went ahead and grab the position
and sent that to the layer below.
We measured the position. If it was less than one,
then we just returned the first name.
Else we returned a sub strain
starting at position one to the position of the location of the Kama.
let's go ahead and go into our function, and it's quite normal to have to ah, you know, move things around a bit and see where you have to
go at it, sort of speak to get the results that you want. So it's normal to like, you know, type in a command that you believe will work and then find out. You need to adjust it a little bit to get it
work the way you want it to work with. That said, Let's go ahead and start typing where we need to do now. I say, if
we need that in quotes is the being quotes. That's how we indicate a string here.
I'm gonna go ahead and close this one off with an end.
we need The first thing we're gonna do is set the sub string to this first piece. So we're gonna copy this.
I'm going to say we're gonna set string.
So the string comes in, we're gonna go ahead and mutate it
or more if it or change it
to save. Ah, variable space. So we have a variable there. We don't need to save its original form.
We're gonna go ahead and use it, and ah, said it and updated as we work through it
on dhe something. Keep take note. Here. You have to use the key words set
A lot of languages. You would just see this. But here in this environment,
we need that set. Keyword.
All right. And next,
Next, we need the ah position.
So we're gonna go ahead and copy this.
Now, we actually have to declare variable for this because you can't just save it in this string.
We're gonna go back to the top of the function. These are required to be at the top.
So that's how we declare variable. We used the key word declare the name of the variable and the type of the variable. We need an end to hold the location.
or not, Aunt. Sorry. Sorry. Said location.
Now, you could all roll this up until one function chain and say variable, but we're gonna spread it out because it's easier to read. And it's easier to digest, especially when you're new to this stuff.
And it doesn't waste too much off memory. Resource is anyway.
Okay, so now we got the string in the location. However,
we need to switch these account names
with the words string
because account name is not defined as anything in this function. So this function will not know what we're talking about. If we use the word account, name
String is because it comes in as a parameter,
so it will know exactly what we're talking about.
this computation, which is ah, finding the location of a space and getting the sub string of that.
And then we gain the location of that.
Bye again. Looking for a comma
on getting a sub string.
I'm doing the same computation we did up here
so we can actually get rid of this, cause it's the same thing.
We can just look at the string.
Right? So next What did we D'oh?
We dropped down a layer.
We analyzed the position. If it was less than one, we just returned what we had.
So let's do that. We're gonna need another if statement. So this is a nested If
We need that key word, then
It'll be our computer to string. If not,
we just need an l statement,
and then we're gonna return.
What are we gonna return?
We need to return this
And we need to replace that first piece name with a string
was again and won't know what that is. And we change the name of this from position to location
on. We need a semicolon there. Don't forget your semicolons also will not work. We need to end this if now that we be careful with your embedded ifs And here because you have to explicitly end them.
So we need to end ifs. We haven't if starting there and enough starting there
and I will leave. This should work. Let's go ahead and run this.
We close up these extra spaces here because I know Deaver doesn't like them. Sometimes
We'll need to see Michael in there after the end. If
Now let's go give it a test,
so I already have it laid out. What? All you need to do is add the parson aim again with a call to the first for choice
and again passing the account name and I a listed as first thing.
Let's go ahead and run that,
and it looks like it produced the same results. We were able to parse this account name and break it up into the last name and the first name
So there's your introduction to functions that we created a
specialized function for the axiomatic, a database that could parse their name, field and break it apart into the first name and last name.
Um, because if you, for some reason needed to, you know, regularly query that ours you really don't want this
or this in all your queries where you're just trying to break the name apart because that that adds unnecessary complexity to a simple idea, which is, Hey, I just want the names. I don't want to be worried about this stuff, so it's when you have something like that that's coming up repeatedly. It's a good idea to move it into a function
so that you could just call that function.
And this is much easier to read and digest.
You can look at it, tell what it's doing. Parse name. Okay,
Obviously it's got something to with the name. There's an argument called first.
I without even look at them function. I would be pretty confident, saying This is getting the first name
How it's doing that I'm not concerned with that. I'm just gonna trust that it works
and, of course, report any problems that I see with it
now. Something keeping in mind with function is they're very. They have a lot of utility. You can put sub quarries in them.
You can return tables of data. You can do a lot of interesting things with functions now something
to Ah, keep in mind when working with them is that you can make them inefficient, just like you can with the views. You could do something that's very, very taxing on the system
and make a query. Take a very long time to finish. So that is something to keep in mind when working with functions and typically, functions that have slow resolution times are dealing with a lot of sub queries. It's not a great idea
to put some queries or even our a lot of sub quarries and a function.
Um, however, sometimes it's it ends up being, Ah, you know, the best thing
to fix the problem and sometimes that's what you need to do. But it's just something to be thinking about when developing these type of solutions
anyways, that completes the function lesson. I hope you enjoyed it, and I will see you in the next lesson. Thank you.