The document provides an overview of JavaScript functions, outlining their definition, syntax, and execution. It discusses the differences between local and global variables, formal and actual arguments, and the role of the return statement in functions. Additionally, it covers function objects, robust parameter handling, and the function constructor for dynamically creating function objects.
www.webstackacademy.com ‹#›
Introduction tofunctions
Passing values to functions
Returning values
Local and Global variables
Functions as objects
Function constructor
Table of Content
www.webstackacademy.com ‹#›
What isfunction?
●
A function is a block of JavaScript code that is defined once but
may be executed, or invoked, any number of times
●
A function can be used to return a value, construct an object, or
as a mechanism to simply run code
●
JavaScript functions are defined with the function keyword
●
Either function declaration or a function expression can be used
www.webstackacademy.com ‹#›
Parts offunctions
●
Name – A unique name given by developer
●
Parameters / arguments – to pass on input values to function
●
Body – A block of statement(s) to be executed
– Local variable declaration
– Flow of computing statement(s)
– Return statement
7.
www.webstackacademy.com ‹#›
Example :
<script>
functionsum (num1, num2)
{
var result; // local variable declaration
result = num1 + num2; // computing sum
return result; // return statement
}
</script>
Function Example
Function name
return value
Formal arguments
Functionbody
Functiondefinition
8.
www.webstackacademy.com ‹#›
<script>
. .. function definition . . .
var x = 3, y = 5, z; // global variable declaration
z = sum (x, y); // calling function for execution
document.write(“The sum of numbers is : “ + z);
</script>
Function Execution
●
Merely defining a function does not result in execution of the
function; it must be called for execution
x and y are actual arguments
9.
www.webstackacademy.com ‹#›
<script>
. .. function definition . . .
var z = sum (4, 7); // passing literals (constants) to function
document.write(“The sum of numbers is : “ + z);
</script>
Function Execution
●
Actual arguments can be variables or literals
10.
www.webstackacademy.com ‹#›
●
Formal argumentsare the names listed within parenthesis
in function definition (also known as function parameters)
●
Formal arguments are initialized through actual arguments
at run time
●
Actual arguments are variables or literals passed to the
function at the time of invocation (call to execute)
●
The formal arguments are visible to function only
Actual Vs formal arguments
11.
www.webstackacademy.com ‹#›
Actual Vsformal arguments
●
The value from actual argument is copied to formal arguments
before executing the body of function
3
5
3
5
x
y
Make a copy
Make a copy
num1
num2
12.
www.webstackacademy.com ‹#›
The returnstatement
●
By default a function returns undefined
●
Return statement is used to return primitive value or reference of an object
●
The return value or reference
– Can be directly passed on to expressions
– Must be collected using assignment operator to store in a variable and
further utilization
●
There could be more than one return statements present in the function;
but, only one value or reference can be returned
●
The function exits after execution of return statement
13.
www.webstackacademy.com ‹#›
Class Work
●
Writea function to find the square of a given number
●
Write a function to find sum of cubes of two numbers
●
Write a function to reverse a number
[ Hint n =12345 output : 54321 ]
●
Write a function to print all numbers between 1 and 100 which
is divisible by given number z
14.
www.webstackacademy.com ‹#›
Local andGlobal Variables
●
Local variables : declared inside the function
●
Global variables: declared outside the function
●
Local variables are visible to function only and can’t be
shared across functions
●
Global variables can be shared across functions
www.webstackacademy.com ‹#›
Function Parameters
●
JavaScriptis a weekly typed language
●
JavaScript function definitions do not specify data types
for parameters
●
JavaScript does not cross check the number of
arguments received against defined parameters
www.webstackacademy.com ‹#›
Arguments Object
●
JavaScriptfunctions have a built-in object called the
arguments object
●
The arguments object contains an array of the arguments
used when the function was called
●
“arguments.length” property returns number of arguments
received by function when it was invoked
www.webstackacademy.com ‹#›
Robust parameterhandling
●
Function object contains length property which tells us
about defined arguments
<script>
function square (num) {
return num * num;
}
document.write(“number of formal arguments = “ + square.length);
</script>
22.
www.webstackacademy.com ‹#›
Robust parameterhandling
●
Checking passed arguments against defined
<script>
function square (num) {
if(square.length != arguments.length)
throw “square function require only one argument”;
return num * num;
}
</script>
23.
www.webstackacademy.com ‹#›
Function Arguments
●
Primitivetypes are passed by value
– Value from primitive type actual argument is copied to formal
arguments
– If a function changes value through formal argument, it does not
change the original value in actual arguments
●
Objects are Passed by Reference
– In JavaScript, object references are values
– Because of this, objects will behave like they are passed by reference
– If a function changes an object property, it changes the original value
24.
www.webstackacademy.com ‹#›
Function constructor
●
TheFunction constructor creates a new Function object
●
The Function() constructor expects any number of string
arguments
●
The last argument is the body of the function; JavaScript
statements are separated from each other by semicolons
●
Calling constructor directly can create functions
dynamically, but suffers from security and performance
25.
www.webstackacademy.com ‹#›
<script>
var fullname= new Function("firstname", "lastname", “return firstname + ‘
’ + lastname;”);
document.write("Full name is " + fullname(“Tenali”, “Raman”));
</script>
Function constructor
Syntax:
var variablename = new Function(Arg1, Arg2..., "Function Body");