JavaScript Functions: Abstraction, User-Defined Functions, and Return Values - Prof. Linda, Study notes of Computer Science

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

Pre 2010

Uploaded on 07/30/2009

koofers-user-o47
koofers-user-o47 🇺🇸

5

(1)

9 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
3/23/2009
1
Chapter 9 – continued
Abstraction and User-Defined Functions
Multiple Inputs
if a function has more than one input,
parameters in the function definition are separated by
commas
input values in the function call are separated by commas
values are matched to parameters by order
1st input value in the function call is assigned to the 1st
parameter in the function
2nd input value in the function call is assigned to the 2nd
parameter in the function
. . .
Multiple Inputs
function OldMacVerse(animal, sound)
// Assumes: animal and sound are strings
// Results: displays corresponding Old
MacDonald verse
{
. . .
}
--------------------------------------------
OldMacVerse("cow", "moo");
OldMacVerse("moo", "cow");
Parameters and Locals
parameters play an important role in functions
they facilitate the creation of generalized computatio ns
i.e., the function defines a formula, but certain values w ithin the
formula can differ each time the function is call ed
technically, a parameter is a
local variable,
meaning it
exists only inside its particular function
when the function is called, memory cells are allo cated 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 o ther variable
when the function terminates, the parameters “go away,” and
their associated memory cells are freed
Parameters and Locals
by default, variables other than parameters are
considered global, meaning they exist and can be
accessed by JavaScript code anywhere in the page
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 acc essible
outside that function, the global variable is access ible
Declaring Local Variables
we have seen that variables are useful for storing
intermediate steps in a complex computation
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 glo bal
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;
pf3

Partial preview of the text

Download JavaScript Functions: Abstraction, User-Defined Functions, and Return Values - Prof. Linda and more Study notes Computer Science in PDF only on Docsity!

Chapter 9 – continued

Abstraction and User-Defined Functions

Multiple Inputs

 if a function has more than one input,

 parameters in the function definition are separated by

commas

 input values in the function call are separated by commas

 values are matched to parameters by order

1 st^ input value in the function call is assigned to the 1st

parameter in the function

2 nd^ input value in the function call is assigned to the 2nd

parameter in the function

Multiple Inputs

function OldMacVerse(animal, sound)

// Assumes: animal and sound are strings

// Results: displays corresponding Old

MacDonald verse

OldMacVerse("cow", "moo");

OldMacVerse("moo", "cow");

Parameters and Locals

 parameters play an important role in functions

 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

exists only inside its particular function

 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

Parameters and Locals

 by default, variables other than parameters are

considered global, meaning they exist and can be

accessed by JavaScript code anywhere in the page

 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

Declaring Local Variables

 we have seen that variables are useful for storing

intermediate steps in a complex computation

 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;

Declaring Local Variables

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); }

since these variables are declared as local, they will

not affect (or be affected by) any variables with the

same names elsewhere in the page

Functions with Return

 displaying results using document.write or alert is

OK for some functions

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);

 a return statement can be added to a function to specify

its output value

 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;

Functions with Return

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;

Newconvert Page

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

Designing Functions

 functions do not add any computational power to the

language

 a function definition simply encapsulates other statements

 still, the capacity to define and use functions is key to

solving complex problems, as well as to developing

reusable code

 encapsulating repetitive tasks can shorten and simplify code

 functions provide units of computational abstraction –

programmer can ignore details

 functions are self-contained, so can easily be reused in

different applications

Designing Functions

 when is it worthwhile to define a function?

 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

 when defining a function, you must identify

 the inputs

 the computation to be performed using those inputs

 the output