Unformatted text preview:

JavaScript IIIJavaScript so farReview by ExampleSlide 4AbstractionUser-defined functionsFunction CallsFunction Call SyntaxFunction definition exampleFunction Definitionsreturn statementSlide 12Example: User-Defined FunctionsDocumentationScope of variablesLocal VariablesLocal/Global Variable ExampleLibrariesExamples of Using LibrariesJavaScript IIIFunctions and Abstraction2JavaScript so farstatementsassignmentfunction callsdata typesnumericstringbooleanexpressionsvariablesoperatorsfunction callsReview by ExampleFrom W3SchoolsJavaScript Examples34Review by Example<script type="text/javascript"> tempInFahr = prompt("Enter temperature (in Fahrenheit):", "32"); tempInFahr = parseFloat(tempInFahr); tempInCelsius = (5/9) * (tempInFahr - 32); document.write("You entered " + tempInFahr + " degrees Fahrenheit.<br />"); document.write("That's equivalent to " + tempInCelsius + " degrees Celsius.");</script>5AbstractionJavaScript has built-in functionsdocument.writeMath.sqrtBenefit of abstractionwe don't have to write thesewe don't have to know how they workFundamental idea in computer science6User-defined functionsOur own abstractionsFahrToCelsiusBenefitscode once, use oftenfix/enhance in one placeadd structure to large programsgroups and names chunks of computation7Function CallsWhen a we use a function in the body of a program, it is a “function call”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.ExampletempInFahr = prompt("Enter temp. (in Fahrenheit):", "32");tempInFahr = parseFloat(tempInFahr);8Function Call Syntaxprompt ( "Enter a number", "0")return value, in this case is a string containing the user inputfunction name parameter #1parameter #2parameter list9Function definition examplefunction FahrToCelsius (tempInFahr){return (5/9) * (tempInFahr – 32);}declarationstartfunction name parameter namesfunction bodystart of bodyend of body10Function DefinitionsFormat of a function definitionfunction function-name( parameter-list ){ declarations and statements}Function name any valid identifierParameter list names of variables that will receive argumentsMust have same number as function callMay be emptyDeclarations and statementsFunction body (“block” of code)11return statementMath.sqrtreturns a valuedocument.writedoes notIf a return statement existsfunction will return a valuewhat value?If notno value12return statementReturning controlreturn statementCan return either nothing, or a valuereturn expression;No return statement same as return;Not returning a value when expected is an errorExampleMath.sqrtreturns a valuedocument.writedoes not13Example: User-Defined Functionsconvert.html14DocumentationFunctions are self-containedmeant to be used elsewhereeventually by othersDocumentation very importantfunction FahrToCelsius (tempInFahr)// Assumes: tempInFahr is a temperature in Fahrenheit// Returns: the equivalent temperature in Celsius{return (5/9) * (tempInFahr – 32);}15Scope of variablesWith functionsdifferent levels of interpretationLocalwhat happens inside of a functionGlobalwhat happens outside of the function16Local VariablesAll variables declared in function are called localDo not exist outside current functionParametersAlso local variablesPromotes reusabilityKeep shortName clearly17Local/Global Variable Exampletaxes.html18LibrariesWe can define a group of functionsusually relatedSeparate file.js extensionLoad using script tag<script type="text/javascript" src="random.js" />Call functions from page19Examples of Using Librariesconvert.jsconvert2.htmlIn Class


View Full Document

DePaul IT 130 - JavaScript3

Download JavaScript3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view JavaScript3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JavaScript3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?