



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
How to define and use user-defined functions in javascript, focusing on temperature conversion from fahrenheit to celsius. It covers the concept of functions as abstractions, user-defined functions, function parameters, and local variables. The document also discusses why and when to define functions, and provides an example of a function without a return statement for displaying verses of a song.
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!
in modern life, we are constantly confronted with complexity we don't necessarily know how it works, but we know how to use it e.g., how does a TV work? a car? a computer?
to use a TV/car/computer, it's not important to understand the inner workings we ignore unimportant details and focus on those features relevant to using it e.g., TV has power switch, volume control, channel changer, …
tempInCelsius = (5/9) * (tempInFahr - 32);
requires remembering the formula every time
freezing = FahrToCelsius(32); current = FahrToCelsius(78);
function FahrToCelsius(tempInFahr) // Assumes: tempInFahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (tempInFahr - 32); }
function FahrToCelsius(tempInFahr) // Assumes: tempInFahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (tempInFahr - 32); }
st
function FahrToCelsius(tempInFahr) // Assumes: tempInFahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (tempInFahr - 32); }
function FahrToCelsius(tempInFahr) // Assumes: tempInFahr is a temperature in Fahrenheit // Returns: the equivalent temperature in Celsius { return (5/9) * (tempInFahr - 32); }
function names follow the same rules as variables: consist of letters, digits, and underscores and must start with a letter note that there may be: more than one parameter, separated by commas no parameters at all, in which case the function name is followed by () more than one statement inside the curly braces (more later)
function FUNCTION_NAME(PARAMETER_1, PARAMETER_2,..., PARAMETER_n) // Assumes: DESCRIPTION OF ASSUMPTIONS MADE ABOUT PARAMETERS // Returns: DESCRIPTION OF VALUE RETURNED BY FUNCTION { STATEMENTS_TO_PERFORM_(AND_RETURN)_THE DESIRED_COMPUTATION; }
function difference(num1, num2) // Assumes: num1 and num2 are numbers // Returns: the absolute difference between num1 and num { return Math.abs(num1 – num2); }
note: if a page requires more than one user- defined function, they can all be enclosed in the same pair of SCRIPT tags
if a particular computation is complex—meaning that it requires extra variables and/or multiple lines to define if you have to perform a particular computation repeatedly within a page
the inputs the computation to be performed using those inputs the output
when the function is called, memory cells are allocated for the parameters and each input from the call is assigned to its corresponding parameter once a parameter has been assigned a value, you can refer to that parameter within the function just as you would any other variable when the function terminates, the parameters “go away,” and their associated memory cells are freed
note: it is possible to use the same name to refer to a local variable and a global variable within the function, the local variable is accessible outside that function, the global variable is accessible
used for variables in the BODY (global variables)
within a user-defined function, the programmer is free to create new variables and use them in specifying the function’s computation however, by default, new variables used in a function are global but what if the same variable name is already used elsewhere?
function IncomeTax(income, itemized) // Assumes: income >= 0, itemized >= 0 // Returns: flat tax (13%) due after deductions { var deduction, taxableIncome, totalTax; deduction = Math.max(itemized, 4150); taxableIncome = Math.max(income - deduction, 0); totalTax = 0.13*taxableIncome
return totalTax; }
a variable declaration is a statement that lists all local variables to be used in a function (usually the first statement in a function) general form: var LOCAL_1, LOCAL_2,.. ., LOCAL_n;
a library is a text file that contains one or more function definitions once the functions are defined in the library, that library can be loaded into pages as needed
you can download the file and store it on your own machine then, simply specify the file name in the SRC attribute (the default is that the file is in the same folder as the Web page that includes it)
if the SCRIPT tags are malformed or the name/address of the library is incorrect, the library will fail to load this will not cause an error in itself, but any subsequent attempt to call a function from the library will produce
“Error: Object Expected” (using Internet Explorer) or “Error: XXX is not a function” (using Navigator), where XXX represents the typed function name
when you use the SRC attribute in a pair of SCRIPT tags to load a code library, you cannot place additional JavaScript code between the tags think of the SRC attribute as causing the contents of the library to be inserted between the tags, overwriting any other code that was erroneously placed there
if you want additional JavaScript code or another library, you must use another pair of SCRIPT tags