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

Julia local Keyword | Creating a local variable in Julia

Last Updated : 02 Sep, 2021
Improve
Improve
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.
‘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. 
Syntax: 
 

var1 = value1
loop condition
    statement
    local var1
    statement
end

Example 1: 
 

Python3




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


Output: 
 

ERROR: LoadError: UndefVarError: x not defined

Above code generates error because the scope of variable x is limited to the scope of for-loop in which it is defined.
Example 2: 
 

Python3




# Julia program to illustrate
# the use of local variable
 
# Defining function
function check_local()
    x = 0
    for i in 1:5
     
        # Creating local variable
        local x
        x = i * 2
        println(x)
    end
    println(x)
end
 
# Function call
check_local()


Output: 
 

2
4
6
8
10
0

In the above code, it can be seen that when the value of x is printed within the loop, the output is as per condition, but when the value of x is printed outside the scope of for-loop, the value of x is again 0 as assigned earlier. This shows that the scope of the local variable remains limited to the block in which it is defined.
 



Similar Reads

Julia global Keyword | Creating a global 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. '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 s
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