Functions, Parameters and Returns
Consider the images and their relation to functions, parameters and returns.
Functions
Functions are used to store small snippets of code.
We need to consider when to use a different function to do different work. Task: Create a button that calls a function that calls a function that says hi. Here are reasons we would separate this into functions:
|
Returns
Functions can return information to the user.
In that case the function becomes like a variable itself. e.g. function isTen () { return 10; } This function literally is the value 10. You could write: isTen() + isTen(); Task 1: Add two numbers together: Create a function called is20 and is30. Call them together in an alert. Task 2: isBlank. Create a blank text box in html Create a function that will check to see if that box is blank. It will return either True or false. |
Brief: Check the form
Create a form with a First Name, Last Name and a check box agreeing to terms and conditions.
Create a function that will check if the first name, last name or check box has data that has been entered.
If not it will return false
If so, it will return true.
If it the function returns false it should present the user with an error.
If it returns true it should say "data stored"
Create a function that will check if the first name, last name or check box has data that has been entered.
If not it will return false
If so, it will return true.
If it the function returns false it should present the user with an error.
If it returns true it should say "data stored"
Parameters
Parameters are used to pass through information for the user.
They are like variables that are only used in the instance of the function. Task: Insult or compliment the person. Create a function that takes the first name of someone. It will return an insult or a compliment. e.g. What is your name? Duncan is smelly. |
Brief: GST Calculator
Create a function that will return the GST amount on any number that is added to it.
The number should be a parameter and it should return that amount.
Alert that amount.
The number should be a parameter and it should return that amount.
Alert that amount.
Brief: HTML Creator
Create a form that asks for a users first name, last name and favorite colour
Send that information through to a function using parameters
Return HTML using the parameters to make HTML that would create this:
First Name: Duncan
Last Name: Tyler
Favorite Colour: Hot Pink
It should include <br>'s and <b>'s
Send that information through to a function using parameters
Return HTML using the parameters to make HTML that would create this:
First Name: Duncan
Last Name: Tyler
Favorite Colour: Hot Pink
It should include <br>'s and <b>'s