Monday 29 February 2016

Julia: return values from function


‘return’ statement is used to return a value from function (or) return immediately from a function.

julia> function process_data(a)
           if(a==10)
               print("Returning immediately")
               return
           end
           print("Doubling a")
           return 2a
       end
process_data (generic function with 1 method)

julia> process_data(10)
Returning immediately
julia> 

julia> process_data(20)
Doubling a40

julia> process_data(200)
Doubling a400

Note
If you don’t return a value from function, by default the value of the last expression is returned.

Return multiple values

You can return multiple values from a function.

julia> function swap(a,b)
           return b,a
       end
swap (generic function with 1 method)

julia> a=10;b=20;

julia> a,b=swap(a,b)
(20,10)

julia> a
20

julia> b
10





Previous                                                 Next                                                 Home

No comments:

Post a Comment