Skip to content

Latest commit

 

History

History
35 lines (27 loc) · 654 Bytes

nested-functions.md

File metadata and controls

35 lines (27 loc) · 654 Bytes
layouttitlelanguagepartofnumnext-pageprevious-page
tour
Ugniježdene metode
ba
scala-tour
9
multiple-parameter-lists
higher-order-functions

U Scali je moguće ugnježdavati definicije metode. Sljedeći objekt sadrži metodu factorial za računanje faktorijela datog broja:

deffactorial(x: Int):Int= { deffact(x: Int, accumulator: Int):Int= { if (x <=1) accumulator else fact(x -1, x * accumulator) } fact(x, 1) } println("Factorial of 2: "+ factorial(2)) println("Factorial of 3: "+ factorial(3))

Izlaz ovog programa je:

Factorial of 2: 2 Factorial of 3: 6 
close