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 farstatementsassignmentfunction callsdata typesnumericstringbooleanexpressionsvariablesoperatorsfunction callsReview by ExampleFrom W3SchoolsJavaScript 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>5AbstractionJavaScript has built-in functionsdocument.writeMath.sqrtBenefit of abstractionwe don't have to write thesewe don't have to know how they workFundamental idea in computer science6User-defined functionsOur own abstractionsFahrToCelsiusBenefitscode once, use oftenfix/enhance in one placeadd structure to large programsgroups and names chunks of computation7Function CallsWhen 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 Syntaxprompt ( "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 DefinitionsFormat of a function definitionfunction function-name( parameter-list ){ declarations and statements}Function name any valid identifierParameter list names of variables that will receive argumentsMust have same number as function callMay be emptyDeclarations and statementsFunction body (“block” of code)11return statementMath.sqrtreturns a valuedocument.writedoes notIf a return statement existsfunction will return a valuewhat value?If notno value12return statementReturning controlreturn statementCan return either nothing, or a valuereturn expression;No return statement same as return;Not returning a value when expected is an errorExampleMath.sqrtreturns a valuedocument.writedoes not13Example: User-Defined Functionsconvert.html14DocumentationFunctions are self-containedmeant to be used elsewhereeventually by othersDocumentation very importantfunction FahrToCelsius (tempInFahr)// Assumes: tempInFahr is a temperature in Fahrenheit// Returns: the equivalent temperature in Celsius{return (5/9) * (tempInFahr – 32);}15Scope of variablesWith functionsdifferent levels of interpretationLocalwhat happens inside of a functionGlobalwhat happens outside of the function16Local VariablesAll variables declared in function are called localDo not exist outside current functionParametersAlso local variablesPromotes reusabilityKeep shortName clearly17Local/Global Variable Exampletaxes.html18LibrariesWe can define a group of functionsusually relatedSeparate file.js extensionLoad using script tag<script type="text/javascript" src="random.js" />Call functions from page19Examples of Using Librariesconvert.jsconvert2.htmlIn Class
View Full Document