Download presentation
Presentation is loading. Please wait.
Published byLeon Merritt Modified over 8 years ago
1
Custom Functions © Copyright 2014, Fred McClurg All Rights Reserved
2
What is a Function? Description: A function is a named block of code that performs a task or an action. A function is executed when it’s name is invoked or called. It is useful for reducing code rewrite, making the code more encapsulated (self- contained), easier to maintain and more reusable. 2
3
Function Syntax Function Definition Syntax: function funcName( p1,... ) {...; // function body return( returnVal ); } Function Call Syntax: var retVal = funcName( p1,... ); 3
4
Function Example Function Definition Example: function increment( value ) { value++; return( value ); } Function Call Example: var num = increment( 3 ); console.log( num ); 4
5
Internal Functions Function definition and function call in the same file “internalFunct.html”: // function definition function increment( value ) { value++; return( value ); } // function call var num = increment( 3 ); console.log( num ); 5 internalFunct.html
6
External Functions Function Definition in file “externalFunct.js”: function increment( value ) { value++; return( value ); } Function Call in file “externalFunct.html”: var num = increment( 3 ); console.log( num ); 6 externalFunct.html externalFunct.js
7
Function Definition: randomInt() /** * Random integer within a range * * @param min Start of range * @param max End of range * @return rndInt Random integer between * min and max inclusive */ function randomInt( min, max ) { var rndInt = Math.floor( ( Math.random() * max ) + min ); // value returned to function call return( rndInt ); } 7 randomInt.html
8
Function Call: randomInt() /* * Generate 10 random numbers */ for ( var i = 0; i < 10; i++ ) { // function call var rndNum = randomInt( 1, 10 ); console.log( i + ": " + rndNum ); } 8 randomInt.html
9
Function Definition: fuddify() /** * Converts string to Elmer Fudd speech * * @param origSpeak Original text * @return fuddSpeak Converted text */ function fuddify( origSpeak ) { var fuddSpeak = origSpeak; if ( typeof( origSpeak ) != "string" ) fuddSpeak = "Can't Fuddify: " + origSpeak; fuddSpeak = fuddSpeak.replace( /r/g, 'w' ); fuddSpeak = fuddSpeak.replace( /R/g, 'W' ); return( fuddSpeak ); } 9 fuddify.html
10
Function Call: fuddify() var quote = "Shhh.\n" + "Be very very quiet.\n" + "I'm hunting rabbits."; // function call var fuddTalk = fuddify( quote ); alert( fuddTalk ); 10 fuddify.html
11
Function Definition: isEven () /** * Determine if number is even * * @param num Passed parameter * @return retVal True if parameter is even */ function isEven( num ) { var retVal; if ( ( num % 2 ) == 0 ) // evenly divisible by 2 retVal = true; else retVal = false; return( retVal ); // ta senda } 11 evenOdd.html
12
Function Definition: isOdd () /** * Determine if number is odd * * @param num Passed parameter * @return True if param odd */ function isOdd( num ) { // call function and negate return( ! isEven( num ) ); } 12 evenOdd.html
13
Function Call: isEven() & isOdd() var str; for ( var i = 0; i < 10; i++ ) { str = "isEven(" + i + "): " + isEven(i) + " " + "isOdd(" + i + "): " + isOdd(i); console.log( str ); } 13 evenOdd.html
14
Default Parameters via Definition A function can default to a certain value if the passed arguments are missing. /** * Calculate the area of rectangle * * @param height Rectangle height * @param width Rectangle width (optional) * @return area Rectangle area */ function areaRectangle( height, width ) { var area; // check if parameter is specified if ( typeof( width ) != 'undefined' ) area = height * width; else area = height * height; return( area ); } 14 defParam.html
15
Default Arguments via Call The function can be called with two or one argument: var height = 5; var width = 10; var areaRt, areaSq; // pass two arguments areaRt = areaRectangle( height, width ); console.log( areaRt ); // pass one argument areaSq = areaRectangle( width ); console.log( areaSq ); 15 defParam.html
16
Variable Length Parameter Definition A function can be made to handle any number of arguments. /** * Sum the passed arguments * * @param arguments Any number of arguments * @return total Summation of arguments */ function add() { var total = 0; for ( var i = 0; i < arguments.length; i++ ) { var num = arguments[i]; if ( typeof( arguments[i] ) == 'number' ) total += num; } return( total ); } 16 varLenArgs.html
17
Variable Length Argument Call A function can be designed to pass any number of arguments. var sum; sum = add( 1 ); // pass one args console.log( sum ); sum = add( 1, 2 ); // pass two args console.log( sum ); sum = add( 1, 2, 3 ); // pass three args console.log( sum ); 17 varLenArgs.html
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.