The Wayback Machine - https://web.archive.org/web/20240717024510/https://www.geeksforgeeks.org/julia-global-keyword-creating-a-global-variable-in-julia/
Open In App

Julia global Keyword | Creating a global variable in Julia

Last Updated : 26 Mar, 2020
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution.

'global' keyword in Julia is used to access a variable that is defined in the global scope. It makes the variable where it is used as its current scope and refers to the global variable of that name.
Syntax:

var1 = value1
loop condition
    statement
    global var1 = value2
    statement
end

Example 1:




# Julia program to illustrate 
# the use of global variable
  
for i in 1:10
    global x = i
end
  
# Accessing global variable 
# from outside of the loop
println(x)


Output:

10

With the use of global variable, the value of variable x can be accessed from anywhere in the code.

Example 2:




# Julia program to illustrate 
# the use of global variable
  
x = 0
  
# Defining function
function check_global()
    for i in 1:5
      
        # Re-defining global variable
        global x = i * 2
        println(x)
    end
    println(x)
end
  
# Function call
check_global()


Output:

2
4
6
8
10
10

In the above code, it can be seen that the loop will redefine the value of the global variable and when it is called outside of the loop, the updated value is printed.



Similar Reads

Julia local Keyword | Creating a local variable in Julia
Keywords in Julia are reserved words whose value is pre-defined to the compiler and can not be changed by the user. These words have a specific meaning and perform their specific operation on execution.'local' keyword in Julia is used to create a variable of a limited scope whose value is local to the scope of the block in which it is defined. Synt
2 min read
Julia end Keyword | Marking end of blocks in Julia
Keywords in Julia are reserved words that have a pre-defined meaning to the compiler. These keywords can't be used as a variable name. 'end' keyword in Julia is used to mark the end of a block of statements. This block can be of any type like struct, loop, conditional statement, module, etc. Syntax: block_type block_name Statement Statement end Exa
1 min read
Julia function keyword | Create user-defined functions in Julia
Keywords are the reserved words in Julia which have a predefined meaning to the compiler. These keywords are used to reduce the number of lines in code. Keywords in Julia can't be used as variable names. 'function' keyword is used to create user-defined functions in Julia. These functions are reusable codes that can be called from anywhere in the c
1 min read
Julia continue Keyword | Continue iterating to next value of a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can’t be used to name variables. 'continue' keyword in Julia skips the statement immediately after the continue statement. Whenever the continue keyword is executed, the compiler immediately stops iterating over further values and sends the execution
1 min read
Julia break Keyword | Exiting from a loop in Julia
Keywords in Julia are predefined words that have a definite meaning to the compiler. These keywords can't be used to name variables. 'break' keyword in Julia is used to exit from a loop immediately. Whenever the break keyword is executed, the compiler immediately stops iterating over further values and sends the execution pointer out of the loop. S
1 min read
if keyword - Conditional evaluation in Julia
Keywords in Julia are reserved words that have a specific meaning and operation to the compiler. These keywords can not be used as a variable name, doing the same will stop the execution process and an error will be raised. 'if' keyword in Julia is used to evaluate a condition. If the condition is true, the code after the if statement will be execu
2 min read
Julia return keyword
Keywords in Julia are used to convey a specific pre-defined meaning to the compiles. These words are reserved words and can not be used as variable names. 'return' keyword in Julia is used to return the last computed value to the caller function. This keyword will cause the enclosing function to exit once the value is returned. Syntax: function fun
1 min read
try keyword - Handling Errors in Julia
Keywords in Julia are words that can not be used as a variable name because they have a pre-defined meaning to the compiler. 'try' keyword in Julia is used to intercept errors thrown by the compiler, so that the program execution can continue. This helps in providing the user a warning that this code is unable to perform the specific operation but
2 min read
else keyword in Julia
Keywords in Julia are reserved words that have a specific meaning and operation to the compiler. These keywords can not be used as a variable name, doing the same will stop the execution process and an error will be raised. 'else' keyword in Julia is used to execute a block of code when the 'if' condition is false. If the condition is true, the cod
2 min read
How to Setup Julia Path to Environment Variable?
Environment variables basically define the behavior of the environment. They can affect the processes ongoing or the programs that are executed in the environment. The region from which this variable can be accessed or over which it is defined is termed as the scope of the variable. In Julia, setting up the environment variable is very important be
2 min read
Article Tags :
three90RightbarBannerImg