Upcoming SlideShare
×

# Scala functions

957 views
906 views

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
957
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
9
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Scala functions

1. 1.    Functions In Scala    Janmejani                                     Software Consultant          Knoldus Software LLP
2. 2. AGENDA● What is Functions.● Local Functions.● First Class Function● Placeholders● Partially Applied Functions● Closures● Repeated Parameters● Tail Recursion
3. 3. What is Function➢ A function is a group of statements that together perform a task.➢ When program gets larger, you need some way to divide them into smaller more manageable pieces.➢ How you divide is up to you, but logically each function perform a specific task.
4. 4. Difference Between Functions And Methods Functions Methods➢ Functions have independent ➢ Methods do not have existence means they can be independent existence defined outside of the class. they are always defined with in class. ➢ Methods are called using➢ Functions are called instance or object. independently.
5. 5. Functions Declaration And Definitiondef functionName ([list of parameters]) : [return type]def functionName ([list of parameters]) : [return type] = { function body return [expr] }def addInt( a:Int, b:Int ) = { var sum = 0 sum = a + b sum }
6. 6. Calling FunctionsFollowing is the standard way to call amethod:object Test { def main(args: Array[String]) { println( "Returned Value : " + addInt(5,7) ) } def addInt( a:Int, b:Int ) : a+b}
7. 7. Local FunctionsScala allows you to define functions inside a function are called localfunctions. def factorial(i: Int): Int = { def fact(i: Int, factor: Int): Int = { if (i <= 1) factor else fact(i - 1, i * factor) } fact(i, 1) }}
8. 8. First Class FunctionsScala supports first-class functions,which means you can expressfunctions in function literal syntax, ie. , (x: Int) => x + 1,A function literal is compiled into a class that when instantiated at run-time is a function value. For eg : var increase = (x: Int) => x + 1 increase(10)
9. 9. Functions Applied On Functionsforeach: It takes a function as an argument and invokes that function on each of its elements.For eg: val someNumbers = List(-11, -10, -5, 0, 5, 10) SomeNumbers foreach((x: Int) => println(x))
10. 10. Filters: Scala provides a number of ways to leave out redundant information. This method selects those elements of a collection that pass a test the user supplies. For eg: someNumbers.filter(x => x > 0) someNumbers.filter(_> 0) To make a function literal even more concise, you can use underscores as placeholders for one or more parameters, so long as each parameter appears only one time within the function literal.
11. 11. Partially Applied FunctionsReplace the entire list of parameter. For example, rather than writing println(_), you could write println _. val someNumbers = List(-11, -10, -5, 0, 5, 10) someNumbers.foreach(println _)A partially applied function is an expression in which you don’t supply all ofthe arguments needed by the function. Instead, you supply some, or none, ofthe needed arguments.
12. 12. Closures A closure is a function whose return value depends on the value of one ormore variables declared outside this function. For eg: val multiplier = (i:Int) => i * 10➢ A statement with no free variable is called close term. val multiplier = (i:Int) => i * factor➢ A statement with free variable is called open term.factor is a free variablei is a bound variableThe function value (the object) that’s created at runtime from this functionliteral is called a closure.
13. 13. REPEATED PARAMETERSScala allows you to indicate that the last parameter to a function may beRepeated.This allows clients to pass variable length argument lists to theFunction. For eg: def Size(is: Int*) = is.length println(Size(2,3,4,5,6,67))To denote a repeated parameter, place an asterisk after the type ofthe parameter.
14. 14. Tail RecursionIn order for a recursive call to be tail recursive, the call back to the functionmust be the last action performed in the function. def factorial(number:Int) : Int = { if (number == 1) return 1 number * factorial (number - 1) } println(factorial(5))This is not a tail recursive Function, because the total returned from therecursive call is being multiplied by number, the recursive call is NOT thelast action performed in the function.
15. 15. To take this example and make it tail recursive, we must make sure that last action performed in the function is the recursive call.def factorial(fact: Int, number: Int) : Int = { if(number == 1) return fact factorial(number * fact, number - 1)}print(factorial(1,5))
16. 16. Why Tail Recursion?In the recursion example, notice how the result of each call must beremembered, to do this each recursive call requires an entry on the stackuntil all recursive calls have been made. This makes the recursive call moreexpensive in terms of memory.While in the tail recursive example, there are no intermediate values thatneed to be stored on the stack, the intermediate value is always passed backas a parameter.