Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Mastering Julia

You're reading from  Mastering Julia

Product type Book
Published in Jul 2015
Publisher
ISBN-13 9781783553310
Pages 410 pages
Edition 1st Edition
Languages

Functions


We have met functions in previous chapters and know how a function() … end block works and that there is a convenient one-line syntax for the simplest of cases:

That is, sq(x) = x*x is exactly equivalent to the following:

function sq(x)
  y = x*x
  return y
end

The variable y is not needed (of course). It is local to the sq() function and has no existence outside the function call. So, the last statement could be written as return x*x or even just as x*x, since functions in Julia return their last value.

First-class objects

Functions are first-class objects in Julia. This allows them to be assigned to other identifiers, passed as arguments to other functions, returned as the value from other functions, stored as collections, and applied (mapped) to a set of values at runtime.

The argument list consists of a set of dummy variables, and the data structure using the () notation is called a tuple. By default, the arguments are of type {Any}, but explicit argument types can be specified,...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime}