

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
The concept of user-defined functions in javascript, focusing on multiple inputs, parameters and locals, declaring local variables, and functions with return values. It also covers the importance of functions in simplifying code and avoiding name conflicts.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


they facilitate the creation of generalized computations i.e., the function defines a formula, but certain values within the formula can differ each time the function is called
technically, a parameter is a local variable, meaning it
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
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? to avoid name conflicts, the programmer should declare temporary variables to be local 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;
function IncomeTax(income, itemized) // Assumes: income >= 0, itemized >= 0 // Results: displays 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
alert("You owe $" + totalTax); }
for full generality, we need to be able to return an output value, which can then be used in other computations
e.g., number = Math.sqrt(9);
amountOwed = IncomeTax(38000, 6500);
when the return statement is reached, the variable or expression is evaluated and its value is returned as the function's output general form: return OUTPUT_VALUE;
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; }
since this function returns the value, it can be used in other computations, e.g., calculate amount owed in 4 payments:
payment = IncomeTax(38000, 6500)/4;
if the same computation must be done repeatedly, a function can greatly simplify the page here, FahrToCelsius is called twice to convert two different temperatures