Home » Julia

Variables in Julia programming language

Julia | Variables: In this tutorial, we are going to learn about the variables in Julia programming language, variable declarations, variable initializations, accessing variables, etc.
Submitted by IncludeHelp, on March 27, 2020

Variables in Julia

Just like other programming languages, in Julia variables are the name of memory blocks that are associated (or bound) to a value. It is useful when a value to be stored or to be accessed in/from memory locations.

Rules to declare a variable

  • A variable name must be started with:
    • a letter (A to Z/a to z), or
    • an underscore (_), or
    • a subset of Unicode code points greater than 00A0
  • A variable name must not contain any special character (exception some of the character in special cases).
  • Digits (0-9) can be used in the variable name (not as a starting letter)

Variable naming conventions

For a better programming style in Julia, we should remember the following points,

  • Variable names should be in lowercase
  • Instead of using space as a word separator in variable names, we should use underscore (_).
  • Types names and Modules names should be started with a capital letter (uppercase), if the variable name is long (used multiple words) then for word separation, we should not use underscore (-), instead of using underscore (_) use the first letter as capital (uppercase) of each word.
  • Functions and macros names should be in lowercase without word separation.

Declaring a variable and Assigning values

Since Julia does not support data type with the global variables, the variable automatically detects its type based on the given values.

Example:

# variables
a = 10
b = 10.23
c = 'c'
d = "Hello"

# printing the values
println("Initial values...")
println("a: ", a)
println("b: ", b)
println("c: ", c)
println("d: ", d)

# updating the values 
a = a + 10
b = b + 12
c = 'x'
d = "world!"

# printing the values
println("After updating...")
println("a: ", a)
println("b: ", b)
println("c: ", c)
println("d: ", d)

Output

Initial values...
a: 10
b: 10.23
c: c
d: Hello
After updating...
a: 20
b: 22.23
c: x
d: world!

Reference: https://docs.julialang.org




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.