Download presentation
Presentation is loading. Please wait.
Published byCarmella Jefferson Modified over 9 years ago
1
Functions Function is a standalone block of statements that performs some tasks and returns a value. Functions must be declared before they can be used. They are usually placed in the tag of the HTML to ensure that they are defined before they are used.
2
Format Function Definition: function function_name () {statement; statement;} function function_name (parameter, parameter) {statement; statement;} Function Call: function_name(); function_name(argument1, argument2)
3
Example 1 A Simple Function function welcome(){ // function definition within HTML head tags var place="San Francisco"; alert("Welcome to "+ place + "!"); alert("welcome() is of type: " + typeof(welcome)); } San Francisco welcome();
4
Example 2 Passing Arguments function greetings(name){ // "Birdman!" is stored in name alert("Greetings to you, " + name); } greetings("Birdman!");
5
Example 3: Calling functions from JavaScript Calling Functions From JavaScript function greetings(name){ // function definition within HTML head tags alert("Greetings to you, " + name); document.bgColor="lightblue"; } In the body of the document. var yourname=prompt("What is your name? ", ""); greetings(yourname);
6
Example 4: Calling a function from a link New Document function greetings(){// function definition within HTML head tags document.bgColor="lightblue"; alert("Greetings to you! "); } Click here for salutations!
7
Example 5:Calling a function from an event calling a function from an event function greetings(){ document.bgColor="lightblue"; alert("Greetings to you!"); } <input type="button" value="Welcome button" onClick="greetings();" >
8
Example 6: Scope of Variables in Functions Function Scope var name="William"; var hometown="Chico"; function greetme(){ // var name="Daniel"; // local variable document.bgColor="lightblue"; document.write(" In function, name is " + name); document.write(" and hometown is "+ hometown); } greetme(); document.write(" Out of function, name is " + name); document.write(" and hometown is " + hometown);
9
Return Values Functions may return values with a return statement. The “return” keyword is optional and can only exist within a function. If the call to the function is made part of an expression, the returned value can be assigned to a variable Format: return; return expression;
10
Example 7 Return Value function mileage(miles, gas) { return miles/gas; // return the result of the division } var distance=eval(prompt("How many miles did you drive? ", "")); var amount=eval(prompt("How much gas did you use?", "")); var rate = mileage(distance, amount); // return value assigned to rate alert("You're mileage "+ rate +" miles per gallon.\n");
11
Recursion A recursive function is a function that calls itself. When a function calls itself, execution starts at the beginning of the function, and when the function ends, the program backs up to where it was when it called the function and starts executing from that point. There must be a way to stop the recursion, or it will be infinite, and probably cause the program to crash.
12
Example 8 Recursion function upDown(num){ document.write(" Level " + num + " "); if(num < 4){ upDown(num + 1); // Function calls itself document.write(" Level "+ num + " "); } Recursion upDown(1);
13
Debugging tips 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 calling 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 the number of parameters? 7- Is the function suppose 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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.