Page 1 of 1

using functions and a good tutorial?

Posted: Thu Jun 19, 2014 1:12 pm
by TubooBokBok
Hello, I have another question.

How do you go about using functions in livecode? I have an if statement where is a boolean is true then one function is called, and if the boolean is false a different function is called. However this code wont work. Both functions just contain if statements that will put a variable into a field. Pretty much all I want to do is add code that will initiate a function, and then the code in the function is executed. No values need to be passed between procedures or anything.

also are there any good livecode tutorials around? I would prefer youtube videos if possible. I have tried to watch the livecode academy video but it is too long to sit through.

Thanks for any help.

Re: using functions and a good tutorial?

Posted: Thu Jun 19, 2014 2:49 pm
by MaxV
Open Livecode, go to Help -> User Guide, it will open a big PDF file (more than 300 pages) with all the information you are looking for.
Moreover it is also on line at: http://www.scribd.com/doc/133073862/LiveCode-User-Guide

Re: using functions and a good tutorial?

Posted: Thu Jun 19, 2014 2:51 pm
by FourthWorld
Have you checked out Chapter 5 in the User Guide? There's some good background on functions there.

Re: using functions and a good tutorial?

Posted: Thu Jun 19, 2014 2:59 pm
by magice
When I started programming, this is one of those subjects that for me was a "sudden click" moment that brought everything together. For that reason I thought I would try to explain it in terms that I would have better understood at that time.Unfortunately that is easier said than done. None the less, here is my attempt at being all teachery (new word I just made up. deal with it. Programmers are allowed to do that)
There are two basic structures for creating custom reusable code, the handler and the function. The handler is the simplest. It is done by simply defining a section within the script with "on" and a name. like so:

Code: Select all


on myHandlerName
	stuff i want the handler to do
	more stuff
	etc.
end myHandlerName
This handler can then be called by its name:

Code: Select all

if blah blah blah
  then
    myHandlerName
end if
A function is a bit more fun. It is generally passed values when it is called, and returns a new value. Here is an example:

Code: Select all

function addMyNumbers tX tY
  return tX+tY
end addMyNumbers
The structure starts with the word function, then the name you choose for your function followed by the names of variables that will store any data to be passed to the function when it is being called. It should have a "return line that determines what value the function call will actually represent, and finally an end line. An example of how you might call this function is as follows.

Code: Select all

put addMyNUmbers(6,3) into tResult
This would put 9 into a variable named tResult. Of course there is no need to write such a function as this example since it is built into LC, but I think it serves to show the basic structure.
another example:

Code: Select all

set the text of field "myField" to addMyNumbers(tFirstNum,tSecondNum)
In this example the number values contained in the variables tFirstNum and tSecondNum are added and put within the field "myField".

Now that i look at it, my attempt to be simple might be just confusing, but it really is a very simple concept. I promise.

Here is a homework assignment. Write a function that returns the average of a list of numbers. Do not use the built in averaging functions in LC (only use addition and division)

Re: using functions and a good tutorial?

Posted: Thu Jun 19, 2014 4:14 pm
by jacque
Here's an article I wrote to explain functions:

http://hyperactivesw.com/resources_function.html

But from your description it doesn't sound like that's what you need. I think you only need a plain handler.

Re: using functions and a good tutorial?

Posted: Thu Jun 19, 2014 4:51 pm
by TubooBokBok
wow thank you so much everyone (especially magice). I didn't even realize that handlers existed :D I will read over those documents you guys posted aswell.