Jump to content
  • 0

Function and values


Starter1
 Share

Question

1 answer to this question

Recommended Posts

  • 0

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()

 

Edited by MAARS
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • 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.