Download presentation
Presentation is loading. Please wait.
1
JavaScript Functions
2
Storing Information in a Computer
3
Holding information Computer doesn’t understand values
It knows about locations: “cubby holes” Takes locations and does what you tell it: read, write, add
4
Assignment Statement A = B + C; Cubby Hole 2: add this value
Cubby Hole 3: place it here A = B + C; Cubby Hole 1: take this value
5
CUBBY HOLES Types of cubby holes Differences Fields in a form
Specifically defined ones: VARIABLES Information that you pass around Differences Some you can only read Some you can only write Some you can do both
6
Changing Information A = A + C; Cubby Hole 2: add this value
Cubby Hole 1: replace the old value A = A + C; Cubby Hole 1: take this value
7
Functions
8
What is a function? Collection of statements that can be invoked as a unit
9
Different Types of Functions
Who writes? JavaScript provides (alert) Someone else writes and you use You write Do they need to be given information? Alert does…tell it what to write Asking for the current date doesn’t Are they a statement or part of an expression? Alert is a statement; doesn’t give you anything back If you ask for the current time, you expect to get something back
10
TODAY JavaScript provides Do they need to be given information?
Both Are they a statement or part of an expression?
11
Output Black Box Inputs
12
Return Parameters Function
13
Functions as Statements
Performs an action but doesn’t give you anything back Example: alert
14
Parameters Call a function with a different value every time
MUST give it everything that it expects For alert One parameter, the string to print
15
Alert String to print Function
16
But What if I Want a Value Back?
For Assignment 7: eval(string) Like alert, takes a string as a parameter Does its magic on the string And gives you back a number You can do what you want with the number Put it in an alert Write it out to a form Do more math on it
17
Using eval formname.fieldname.value = eval(‘3+5’);
alert(eval(formname.inputfield.value));
18
Format of a Function functionName(parm,parm);
Always needs () even if no parameter functionName();
19
The rules The order of the parameters matter Only a single output
5 - 3 different than 3 -5 Any number of parameters can be defined Fixed for any specific function Only a single output Optional in general But always the same for any specific function
20
Random Numbers
21
Why random numbers? Variability Different results Examples
Display one of ten pictures Display one of five quotations Play a game of chance All built on a random number!
22
Can computers really create random numbers?
Computers do as they are told So how can they do something random? They can’t! But we can tell them to find a number that changes often! Needs to not have an easily recognized pattern Pseudo-random Number Generator Options? Time!
23
Getting a random number
Math.random() Returns a value between 0 and 1 One of a series of mathematical functions members of a collection called Math each are prefixed by “Math.”
24
What if you don’t want 0-1? Change range? Multiply Not start at 0? Add
Integers only? Round Floor Ceiling
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.