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

Julia: Create anonymous functions using do block

In previous post, I explained anonymous functions. You can create anonymous function using do statement also.

map((x) -> x^3, [1, 2, 3, 4, 5])

Above anonymous function is used to calculate x^3. Same anonymous function can be written like below.

map([1, 2, 3]) do x
    x^3
end
julia> map([1, 2, 3]) do x
           x^3
       end
3-element Array{Int64,1}:
  1
  8
 27

julia> map((x) -> x^3, [1, 2, 3, 4, 5])
5-element Array{Int64,1}:
   1
   8
  27
  64
 125

do x : Create an anonymous function with argument x.
do x, y : Create two argument anonymous function.


Previous                                                 Next                                                 Home

Sunday, 28 February 2016

Julia: Anonymous functions

Function without name is called anonymous function.

Syntax
() -> expression    (zero argument anonymous function)
var -> expression  (One argument anonymous function) 
(var1,var2,var3) -> expression (Multi argument anonymous function)

julia> ()->12
(anonymous function)

julia> x->x^2+2x+10
(anonymous function)

julia> (x,y,z)->2x+3y+5z
(anonymous function)

Where can I use anonymous functions
In Julia, you can pass a function as an argument to other function. For example, you can pass a function as an argument to map function.

julia> map((x) -> x^2, [1, 2, 3, 4, 5])
5-element Array{Int64,1}:
  1
  4
  9
 16
 25

julia> map((x) -> x^3, [1, 2, 3, 4, 5])
5-element Array{Int64,1}:
   1
   8
  27
  64
 125



Previous                                                 Next                                                 Home

Julia: Using operators as functions


In Julia, most of the operators can be used as functions. Observe following Julia snippet.
help?> +
search: + .+

  +(x, y...)

  Addition operator. x+y+z+... calls this function with all arguments, i.e. +(x, y, z, ...).

help?> -
search: - .-

  -(x, y)

  Subtraction operator.

  -(x)

  Unary minus operator.

help?> *
search: * .*

  *(x, y...)

  Multiplication operator. x*y*z*... calls this function with all arguments, i.e. *(x, y, z, ...).

  *(s, t)

  Concatenate strings. The * operator is an alias to this function.

  julia> "Hello " * "world"
  "Hello world"

  *(A, B)

  Matrix multiplication

help?> /
search: / // ./ .//

  /(x, y)

  Right division operator: multiplication of x by the inverse of y on the right. Gives floating-point results for integer arguments.


You can use almost all operators other than (&&, ||) as functions in Julia.
julia> +(20,30,40)
90

julia> -(20,30)
-10

julia> *(10,20,30)
6000

julia> /(30,10)
3.0

julia> \(30,10)
0.3333333333333333

julia> <<(2, 4)
32

julia> >>(32, 4)
2


Since operators in Julia are functions, you can assign them to some variables and use them.
julia> sum=+
+ (generic function with 171 methods)

julia> sub=-
- (generic function with 204 methods)

julia> mul=*
* (generic function with 138 methods)

julia> div=/
/ (generic function with 48 methods)

julia> sum(10, 20, 30)
60

julia> sub(10, 20)
-10

julia> mul(10, 20)
200

julia> div(10, 20)
0.5






Previous                                                 Next                                                 Home