This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
kaj's profile picture

Making your own Function with parameters

Started by kaj, 20 July 2012 - 11:02 PM
kaj #1
Posted 21 July 2012 - 01:02 AM
Complete noob question, I know.
And I'm ashamed to be asking it.
I'v just started messing around with cc in the last week, had been experimenting with java previously.
just about got how to make/edit and run simple programs in cc.
Now I want to simplify the code by making abbreviations of existing functions,
and making my own functions, again to complement the existing ones.
so at this point i have 2 questions.

How do I make a function()?

and how do I set up 'global' booleans/ other variables (the way you would in java with public static boolean etc)

If there is an obvious tutorial or a thread out there could someone please post a link.

thanks in advance

-kaj
Lyqyd #2
Posted 21 July 2012 - 01:05 AM

function thisIsMyFunction(x, y)
    --do stuff
end

Why do you want to make globals? There are very few cases where they are needed.
Grim Reaper #3
Posted 21 July 2012 - 01:27 AM
In Java, public is an access specifier for a variables and methods within a class. Lua is not initially Object Oriented, however you can add Object Oriented design fairly easily.

Also, static is to create a variable that is the "same" no matter where you call it. (If that makes much sense.)
In methods and variables that are altered via the 'static' keyword, they are callable and usable without an instance of the class they are defined in. You can replicate this in Lua via OOP, which again, is easily achieved.
Check this Lua OOP tutorial out: Lua Object Oriented Programming Tutorial

As far as I know, Lua only offers one access specifier: 'local'.

The 'local' keyword allows for duplicates of global variables within functions and limiting the access of variables within an API when it is being included by another program.

To create a global variable that is accessible from any file or function simply declare a variable outside of any function in your program without the 'local' variable preceding it.

To create a function, as already stated by Lyqyd, you can do the following:

function testFunc(param1, param2, --[[ so on ]]--)
  -- Code goes here
end

If you already hadn't noticed, Lua does not require a return type in a function declaration.
Functions can also be modified with the 'local' keyword to limit access of them.

You should read into the Lua manual if you have more questions.

Hope I helped! :)/>/>
kaj #4
Posted 21 July 2012 - 02:58 AM
Thank you both very much.
Didn't realise it was as simple as 'edit func()'.
i'v just succeeded in making a function that prints 'func test' in the lua interactive part,
and managed to call that in a program to print it onscreen,
woo.
@lyquyd: I want the eventual system-thing i will eventually try to make to have certain counters and flags that can get changed as different things get run.
ideally i'd also want to access these values/bools from different programs/functions (and use them for stuff). hence globular.

@reaper : you make perfect sense. favorited link :)/>/>.
I am however still a bit unclear on where to declare the variables.
I'm assuming if I put them in startup that would do it?
could they then be changed by programs / functions later on?

again thanks

-kaj
Grim Reaper #5
Posted 21 July 2012 - 06:22 AM
Programs are reset at runtime and when the program finishes execution. To keep the variables changing you would have to save them to a file and be read every time you run the program to get different results. You declare the variables at the top of your program (ideally) without any access specifier.


sString = "Testing!" -- Global variable
function printString( s )
  print( s )
end

The 'startup' file is simply the file that is executed upon boot up or 'startup' if you will.

Other programs can change the variables within another program or file, however these changes are temporary and may only occur if you are using another program or file as an API to reference variables or functions from it.
kaj #6
Posted 21 July 2012 - 05:57 PM
I'm sorry this is turning into a bit of a q+a,
and may only occur if you are using another program or file as an API to reference variables or functions from it.
so it would be doable for me to make a program or function(that would run on startup ideally) - which contains only the variables/bools to be later used/changed by other programs, much in the way you would make a seporate class in java to store bools/vars etc?
I will have to look into apis.

thanks again.

-kaj
Lyqyd #7
Posted 21 July 2012 - 09:18 PM
I'm sorry this is turning into a bit of a q+a,
and may only occur if you are using another program or file as an API to reference variables or functions from it.
so it would be doable for me to make a program or function(that would run on startup ideally) - which contains only the variables/bools to be later used/changed by other programs, much in the way you would make a seporate class in java to store bools/vars etc?
I will have to look into apis.

thanks again.

-kaj

You really probably want to look through the Program Library for any of the persistence APIs. Find one that suits your fancy; they seem to be what you're looking for.
Grim Reaper #8
Posted 22 July 2012 - 05:23 AM
In the case that you're not searching for persistent variables, Java resets variables (like all program languages) upon runtime and completion. So you would need files or persistent variables (thanks Lyqyd): O'Reily's Persistent API

If you just mean having all of your variables and flags in one place then the top of your program would be ideal:

-- Boolean Flags --
flag1 = false
flag2 = false
flag3 = false
-- etc.
-----------------------

-- Ep1C C0d3