using functions and a good tutorial?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
TubooBokBok
Posts: 19
Joined: Wed Jun 18, 2014 7:23 pm

using functions and a good tutorial?

Post by TubooBokBok » Thu Jun 19, 2014 1:12 pm

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.

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: using functions and a good tutorial?

Post by MaxV » Thu Jun 19, 2014 2:49 pm

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
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10053
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: using functions and a good tutorial?

Post by FourthWorld » Thu Jun 19, 2014 2:51 pm

Have you checked out Chapter 5 in the User Guide? There's some good background on functions there.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: using functions and a good tutorial?

Post by magice » Thu Jun 19, 2014 2:59 pm

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)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: using functions and a good tutorial?

Post by jacque » Thu Jun 19, 2014 4:14 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

TubooBokBok
Posts: 19
Joined: Wed Jun 18, 2014 7:23 pm

Re: using functions and a good tutorial?

Post by TubooBokBok » Thu Jun 19, 2014 4:51 pm

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.

Post Reply