Monday, August 28, 2017

Julia - Language - Functions - Passing functions as arguments

When we pass a function as a argument, the functional argument will actually call the function. Let us write and example, where we can see its usage:

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> function myStringFunc(xStr)
       yStr = xStr()
       println("My String is $yStr\!")
       end
myStringFunc (generic function with 1 method)

Creating a second function to embed into the first function's argument:

julia> function myInputFunc()
        return(":: julialang.com")
       end
myInputFunc (generic function with 1 method)

julia> myStringFunc(myInputFunc)
My String is :: julialang.com!

julia> 


Hence, it is possible to embed function inside a function argument. 

No comments:

Post a Comment