Jump to content
  • 0

Function and values


Starter1

Question

1 answer to this question

Recommended Posts

you have multiple way to acomplish this.

1. you can declare the variable outside of a function so you can access it everywhere
 

local a = 10

local function foo ()
  print (a) -- 10
end


local function bar ()
  print (a) -- 10
end

bar()

2. you can use the return statement inside a function to pass a value

local function foo ()
  local a = 10
  return a
end


local function bar ()
  local a = foo()
  print (a) -- 10
end

bar()

3.  In your code you have declared the variable a as a global since you did not use the local keyword. in this case once you run your function one time the variable a will be avalaible averywhere in the script

local function foo()
  a = 10
end
foo()

local function bar()
  print (a) -- 10
end

bar()

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.