Monday, 7 March 2016

Julia: function inside a function


In Julia, you can define a function inside another function.

julia> function arithmetic(a, b)
                 function sum(a ,b)
                     a+b
                 end

                 function subtract(a, b)
                     a-b
                 end

                 function mul(a, b)
                     a*b
                 end

                 function div(a, b)
                     a/b
                 end

                 r1 = sum(a, b), subtract(a, b), mul(a,b), div(a,b)
       end
arithmetic (generic function with 1 method)

julia> addition, subtraction, multiplication, division = arithmetic(20, 10)
(30,10,200,2.0)

julia> addition
30

julia> subtraction
10

julia> multiplication
200

julia> division
2.0



Previous                                                 Next                                                 Home

No comments:

Post a Comment