‫All right.
‫So now we're going to start to get into functions which are an extremely important part of programming.
‫So a function is a block of code that performs a specific task.
‫And we can call that function multiple times throughout our code.
‫And this allows us to reuse code and keep our code organized.
‫So the basic layout or basic signature for a function is this.
‫So we use the function keyword.
‫And then we give it a name in this case function name.
‫And then we open up a set of parentheses that may or may not have parameters.
‫So parameters allow you to pass in arguments that can change how the function works.
‫Sometimes they're they're mandatory.
‫Sometimes they're optional.
‫We'll get into parameters very soon.
‫And then within the curly braces is the is the code to be executed when that function is called.
‫Okay.
‫Now just doing this doesn't actually do anything.
‫It just creates the function.
‫When you actually want to call the function, you would then use the function name with parentheses.
‫And then that would call whatever code is being executed in here.
‫And you can call it multiple times throughout your project.
‫All right.
‫So let's jump into our file here and we'll open up.
‫Whoops.
‫What did I do.
‫We'll open up some PHP tags.
‫And this is going to be just very very simple stuff.
‫I'm sure that a lot of you guys already know this, but we're just going to create just a very basic
‫function.
‫I'll call it say hello.
‫And you can see it's formatted just like the slide.
‫This one, this function doesn't take in any parameters or arguments.
‫And I'll talk about the difference between parameters and arguments in a little bit.
‫So let's just have this echo something.
‫So we'll say echo hello echo hello world.
‫All right.
‫And obviously if I save this and I run it it doesn't do anything.
‫And by the way, prettier is formatting the function so that the the opening curly brace is under this
‫function keyword.
‫I don't like that.
‫I usually format it like this, but for some reason prettier when I save is putting it under there.
‫So I just want to mention that I don't usually use this syntax.
‫A lot of people do.
‫I don't prefer it.
‫So like I said, this isn't doing anything we have to call the function.
‫So let's come down here and let's say say hello, open up some parentheses, reload.
‫And now we see Hello World.
‫And I can call this as many times as I want.
‫So that will show hello world three times.
‫Now function names follow the same rules as variable names.
‫They can only contain letters, numbers, and underscores.
‫They cannot start with a number.
‫So if I try to make this.
‫One say hello.
‫You'll see.
‫I already get an error here.
‫And if I reload, I'm going to get an error.
‫But I can put a number at the end.
‫I can also put an underscore and that is a valid function, although it's giving me an error because
‫now that doesn't exist.
‫So if I reload we have no errors.
‫All right.
‫I'm just going to put that back.
‫Now it's important to understand return values.
‫So right now this function all it's doing is is echoing something out.
‫But normally I shouldn't say normally but in most cases your function is going to return something.
‫So let's change this actually let's create another one.
‫Just so you have both examples.
‫So let's say say say goodbye okay.
‫So we have a function called say goodbye.
‫And instead of echoing goodbye I'm going to return a string of goodbye okay.
‫And then I'm going to come down here and let's, we'll just get rid of these for now and let's say say
‫goodbye.
‫And if I reload, you might expect it to print out.
‫Say.
‫Print out.
‫Goodbye.
‫But it doesn't because we're not echoing it out.
‫We're returning it.
‫Meaning we're saving it in this function.
‫So what I could do is echo the function.
‫All right.
‫Because now what happens is this function, it includes this goodbye string so I can echo it.
‫But what you usually see is you'll set a variable.
‫So we could say.
‫Goodbye as a variable and set it to that the return value of that say goodbye function.
‫And then I could do what I want with that goodbye variable.
‫For instance, echo it out.
‫And there we go.
‫Okay, so it's important to understand that most of the time functions will return something.
‫And then whatever that return value is, you can use that whether you want to store it in a variable
‫or just print it out, whatever it is you want to do with it.
‫Now in the next lesson, we're going to get more into parameters that we can pass in here.