A function in a programming language that HP is a self-contained block of statements that can be used
repeatedly in a program.
They have a way of encapsulating code that performs a certain task into a reusable block to create a
function in HP.
We use the function key word followed by the name of the function the name can be anything you like
but it makes sense to give it a meaningful name and name that reflects what the function does.
Then we have some brackets where we put the functions arguments which we'll get to in a moment.
Then some curly braces inside these braces we put the code that we want to execute when the function
is kalid.
Then we call the function by putting its name followed by brackets.
Let's look at an example.
We'll create a new file and can't function.
Examples p s p
will create a function called show message which is just going to eke out a string.
Then we'll call that function like that.
When we opened this script in a browser the message is displayed.
We can also pass data to a function using arguments are parameters.
Arguments are specified after the function name inside the brackets.
A function can have as many arguments as you like.
Separated by commas inside the function.
These arguments are available just like regular variables when we call the function.
We need to pass in values for these arguments inside the brackets.
Let's add an argument to our example function.
Then inside the body of the function we can access the value of this argument just like a variable.
Then when we call the function we need to pass in a value for this argument like that.
In the browser we see the message coming from the function.
With the argument.
When we declare a function with one or more arguments we can include a default value for them.
This means that when we call the function we can emit that argument and its value will be the default
we supplied.
We can still pass in an argument and override this default however.
If we make the arguments to our function optional by giving it a default value we can still call the
function with an argument and that the value it uses.
But now we can omit the arguments like that and the function uses the default value.
We can also return a value from a function.
Any variables declared inside a function are not available outside of it to access data outside of the
function.
We need to return a value from it to return a value reused a return statement followed by the value
we want to return.
When we call the function the return value can be accessed by using it directly are by assigning it
to a variable.
The return statement returns immediately from the function.
Any code after the return statement isn't executed.
Let's change the function to return a message.
Instead of printing it out by returning the string like that.
Then when we call the function will assign its return value to a variable.
Then print that variable out like that.
And in the browser the result is the same.
You can also have multiple return statements in the same function.
If we change the argument of our function to a billion and then inside the body of the function add
an if statement that returns one message if the argument is true and another if it's false when we call
the function and passin true in the browser we get one message.
If we change the argument to false.
We get the other message.
BHP has hundreds of built in functions and they're all documented in the P HP documentation for example
mathematical functions functions for processing strings and so on the documentation for each function
has full details on their arguments.
What they do and any return values.
As for naming standards there aren't any rules about how standalone functions should be named.
You can name functions however you like for example like this which is called Camel case.
All like this separating words with underscores.
Which ever method you choose.
The important thing is to be consistent.
I prefer to use camel case like this.
Because as we'll see later on when we look at objects printed P 8 p.
There is a standard for naming functions inside classes.
Finally I want to mention comments specifically comments about using P 8 p Duck are p s p documentor
syntax by adding a description of a function using a P E P duck comment.
Not only is it easy to see what the function does what its arguments are and what its return value is.
There are a couple other advantages too.
First you can burn the p p document to program on your code and it will after magically create documentation
for it.
Also lots of code editors will use the P2P documents to after all complete your code.
Use type hinting and so on.
Plus as p s p doc is so widely used or the developers will be easily able to understand your code and
vice versa.
Let's add a P It p duck comment to this function.
It contains a description of what the function does what its arguments are including their types and
the return value as we add functions throughout the rest of this course albhy including p s p documents
for each one.