Java Script III Functions and Abstraction Java Script

Java. Script III Functions and Abstraction

Java. Script so far l statements l l l data types l l assignment function calls numeric string boolean expressions l l l variables operators function calls 2

Review by Example From W 3 Schools l Java. Script Examples 3

Review by Example <script type="text/javascript"> temp. In. Fahr = prompt("Enter temperature (in Fahrenheit): ", "32"); temp. In. Fahr = parse. Float(temp. In. Fahr); temp. In. Celsius = (5/9) * (temp. In. Fahr - 32); document. write("You entered " + temp. In. Fahr + " degrees Fahrenheit. "); document. write("That's equivalent to " + temp. In. Celsius + " degrees Celsius. "); </script> 4

Abstraction l Java. Script has built-in functions l l l Benefit of abstraction l l l document. write Math. sqrt we don't have to write these we don't have to know how they work Fundamental idea in computer science 5

User-defined functions l Our own abstractions l l Fahr. To. Celsius Benefits l l code once, use often fix/enhance in one place add structure to large programs groups and names chunks of computation 6

Function Calls l When a we use a function in the body of a program, it is a “function call” l l Use the name of the function along with values for its parameters. Function call acts as a black box & returns a value. The returned value can be stored in some variable. Example temp. In. Fahr = prompt("Enter temp. (in Fahrenheit): ", "32"); temp. In. Fahr = parse. Float(temp. In. Fahr); 7

Function Call Syntax l prompt ( "Enter a number", "0") function name parameter #1 parameter #2 parameter list l return value, in this case is a string containing the user input 8

Function definition example function Fahr. To. Celsius (temp. In. Fahr) declaration start function name parameter names { start of body return (5/9) * (temp. In. Fahr – 32); function body } end of body 9

Function Definitions l Format of a function definition function-name( parameter-list ) { declarations and statements } l l l Function name any valid identifier Parameter list names of variables that will receive arguments l Must have same number as function call l May be empty Declarations and statements l Function body (“block” of code) 10

return statement l Math. sqrt l l document. write l l does not If a return statement exists l l l returns a value function will return a value what value? If not l no value 11

return statement l Returning control l return statement l Can return either nothing, or a value return expression; l l l No return statement same as return; Not returning a value when expected is an error Example l Math. sqrt l l returns a value document. write l does not 12

Example: User-Defined Functions l convert. html 13

Documentation l Functions are self-contained l l l meant to be used elsewhere eventually by others Documentation very important function Fahr. To. Celsius (temp. In. Fahr) // Assumes: temp. In. Fahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (temp. In. Fahr – 32); } 14

Scope of variables l With functions l l Local l l different levels of interpretation what happens inside of a function Global l what happens outside of the function 15

Local Variables l All variables declared in function are called local l l Parameters l l Do not exist outside current function Also local variables Promotes reusability l l Keep short Name clearly 16

Local/Global Variable Example l taxes. html 17

Libraries l We can define a group of functions l l Separate file l l . js extension Load using script tag l l usually related <script type="text/javascript" src="random. js" /> Call functions from page 18

Examples of Using Libraries l convert. js convert 2. html l In Class Practice: l l ftok. html 19
- Slides: 19