Skip to Main Content
Think Julia
book

Think Julia

by Ben Lauwens, Allen B. Downey
April 2019
Beginner content levelBeginner
295 pages
5h 48m
English
O'Reilly Media, Inc.
Content preview from Think Julia

Chapter 3. Functions

In the context of programming, a function is a named sequence of statements that performs a computation. When you define a function, you specify the name and the sequence of statements. Later, you can “call” the function by name.

Function Calls

We have already seen one example of a function call:

julia> println("Hello, World!")
Hello, World!

The name of the function is println. The expression in parentheses is called the argument of the function.

It is common to say that a function “takes” an argument and “returns” a result. The result is also called the return value.

Julia provides functions that convert values from one type to another. The parse function takes a string and converts it to any number type, if it can, or complains otherwise:

julia> parse(Int64, "32")
32
julia> parse(Float64, "3.14159")
3.14159
julia> parse(Int64, "Hello")
ERROR: ArgumentError: invalid base 10 digit 'H' in "Hello"

trunc can convert floating-point values to integers, but it doesn’t round off; it chops off the fraction part:

julia> trunc(Int64, 3.99999)
3
julia> trunc(Int64, -2.3)
-2

float converts integers to floating-point numbers:

julia> float(32)
32.0

Finally, string converts its argument to a string:

julia> string(32)
"32"
julia> string(3.14159)
"3.14159"

Math Functions

In Julia, most of the familiar mathematical functions are directly available. The following example uses log10 to compute a signal-to-noise ratio in decibels (assuming that signal_power and noise_power ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Practical Julia

Practical Julia

Lee Phillips
Getting Clojure

Getting Clojure

Russ Olsen
Learning Go

Learning Go

Jon Bodner

Publisher Resources

ISBN: 9781492045021Errata PageSupplemental Content