0% found this document useful (0 votes)
16 views

06 Functions

Functions in JavaScript are independent blocks of code that perform tasks. Functions are defined using the function keyword and name and can accept parameters. Functions are invoked by calling them by name. Variables declared inside functions have local scope, while those outside have global scope. Functions can return values using the return statement. Exceptions can be handled using try/catch blocks.

Uploaded by

Hermie Coso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

06 Functions

Functions in JavaScript are independent blocks of code that perform tasks. Functions are defined using the function keyword and name and can accept parameters. Functions are invoked by calling them by name. Variables declared inside functions have local scope, while those outside have global scope. Functions can return values using the return statement. Exceptions can be handled using try/catch blocks.

Uploaded by

Hermie Coso
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Functions

• self-contained units of a program designed


to accomplish a specified task

• independent part of your program and not


executed until called referred to as a “black
box”

• “function” and “method” are often used


interchangeably
• A method refers to a function that is used with
JavaScript objects.

• A stand-alone block of statements,


independent of the program until invoked by a
caller.
• To define a function:
• use function keyword
• followed by the name of the function and a set of
parentheses
• The parentheses are used to hold parameters, values that are
received by the function.

• JavaScript functions are invoked by calling the


function. Example, bye().
• Function can be called from a link, by using the
JavaScript pseudoprotocol, JavaScript:,

• JavaScript: protocol and the function call are placed


within quotes and assigned to the href attribute of
the <a> tag.
• The function assigned to the event is called an
event handler.

• When the event is triggered, the function is


called.
•scope of a variable
• describes where the variable is visible in the
program

• global in scope
• variables declared outside of functions
• they can be used or changed anywhere in the program
• Variable is global if declared within a function unless it is
declared within a function with the var keyword.
•local in scope
• variable can be used only within the function
where it is defined and is no longer visible once
the function ends
• Functions can return values with a return statement.

• used to send back the result of some task or to exit a


function early if some condition occurs

• When the return keyword is reached in a function, no further


processing within the function occurs.
• The value of variable is a function definition and this
variable is used as a reference to the function.

• The () is a JavaScript operator, indicating that a function


is to be called.
• A closure is an a anonymous function defined
within another function.
• The local variables can remain accessible to the inner
function even when it seems they should have gone
out of scope.
• A recursive function is a function that calls itself.
• Show example
• Function Syntax
1. Did you use parentheses after the function name?
2. Did you use opening and closing curly braces to
hold the function definition?
3. Did you define the function before you called it?
Try using the typeof operator to see if a function
has been defined.
4. Did you give the function a unique name?
5. When you called the function is your argument list
separated by commas? If you don’t have an
argument list, did you forget to include the
parentheses?
6. Do the number of arguments equal to the number
of parameters?
7. Is the function supposed to return a value? Did you
remember to provide a variable or a place in the
expression to hold the returned value?
8. Did you define and call the function from within a
JavaScript program?
• The try/catch Statements

• If an exception occurs within the try block, control


will shift to the catch block.

• When an exception is thrown in the try block, the


variable shown as e in catch(e) holds the value of the
type of exception that was thrown in the try block.
Error Name When It Is Raised
EvalError If the eval() function is used in an incorrect manner
RangeError If a numeric variable or parameter exceeds its
allowed
range
ReferenceError If an invalid reference is used; e.g., the variable is
undefined
SyntaxError If a syntax error occurs while parsing code in an eval()
TypeError If the type of a variable or parameter is a valid type
URIError Raised when encodeURI() or decodeURI() are passed
invalid parameters
• The throw Statement
• allows you to create your own conditions for
exceptions

• In the catch block you can create customized error


messages to correspond to a particular error
The following example is written in the C
language. It is a very common recursive function
call factorial. Can you write it in JavaScript?

unsigned int factorial(unsigned int n){


if (n <= 1) {
return 1;
} else {
return n * factorial(n-1);
}
}
END of DISCUSSION

You might also like