YEAR 12 DIGITAL TECHNOLOGY
  • Home
  • Website
    • Learn Basic HTML
    • Learn Basic CSS
    • HTML/CSS Advanced
    • Photoshop Level 2
    • Recap and Review
    • Conventions of Web Design
    • AS91893 - Digital Media Outcome >
      • AS91893 - Resources
  • Programming
    • JavaScript Recap
    • JavaScript and HTML
    • Functions, Parameters and Returns
    • Learn Arrays
    • AS91896 - Brief
  • Databases
    • Intro to Access
    • Relationships and lookups
    • Relationships Extended
    • Queries and Reports
    • Mail Merge
    • Car Sales
    • Documentation x5
    • Importing Data
  • External
  • Freyberg Digital

Functions, Parameters and Returns

Consider the images and their relation to functions, parameters and returns.
Picture
Parameters
Picture
Function
Picture
Return

​Parameters is information we pass through to a function
The function is the code that processes the parameters
​Then returns the information.

​Check the example to the right
Picture
Picture
Picture

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:
  1. Reuse it: If you find yourself doing the same task again and again, make a function for it so you can use it whenever you need.
  2. Make it easier to read: When your code gets too long or hard to understand, break it into smaller parts with clear names so you can tell what each part does.
  3. Test it easily: Functions are like building blocks. If you make each one small and clear, it's easier to check if they work correctly.
  4. Give it a name: If you can name a piece of code something descriptive, it's probably a good idea to turn it into a function. This makes it easier to talk about what your code does.
  5. Keep it simple: Each function should do just one thing. If it's doing too much, it's time to split it into smaller functions.
Picture

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.
Picture
Picture
Picture

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"

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.
Picture

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.

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
Powered by Create your own unique website with customizable templates.
  • Home
  • Website
    • Learn Basic HTML
    • Learn Basic CSS
    • HTML/CSS Advanced
    • Photoshop Level 2
    • Recap and Review
    • Conventions of Web Design
    • AS91893 - Digital Media Outcome >
      • AS91893 - Resources
  • Programming
    • JavaScript Recap
    • JavaScript and HTML
    • Functions, Parameters and Returns
    • Learn Arrays
    • AS91896 - Brief
  • Databases
    • Intro to Access
    • Relationships and lookups
    • Relationships Extended
    • Queries and Reports
    • Mail Merge
    • Car Sales
    • Documentation x5
    • Importing Data
  • External
  • Freyberg Digital