UNDERSTANDING FUNCTIONS

           


WHAT ARE FUNCTIONS ?



Functions enhance the re usability of code . You can keep your code in a function and use it several times . For example :

function hi
now "hello" print
fig


In this example , the name of the function is "hi" . "fig" marks the end of the function The function prints "hello" when it is accessed like this:

var hi

a function call in fig language needs a variable to call a function . In python you can call a function like this :

hi()

where "hi" is the function name .

You can also give parameters to you function i.e, you can give functions values or data it can work with . For example :

function hi message
now message print
fig

var hi "Hello Pam"
var hi "Hello Jim"

OUTPUT:
Hello Pam
Hello Jim


You can use return command in functions to return a value like this :

function hi name

now "hello" plus name return now
fig

var hi "Pam" print

OUTPUT:
hello Pam


The function will return the value of now variable


UNDERSTANDING WITH GAME!



WELCOME TO THE SCHRUTE FARMS! The Schrute farm belongs to Dwight Schrute (an esteemed employee of Dunder Mifflin) . Now we use functions to give our mini character tasks . For example : LEFT , RIGHT, UP, DOWN, BEET etc are all the tasks Dwight has to perform .

So when we click the LEFT button , we are calling a function which moves dwight towards the left , like this:

function moveLEFT
task "move left"
dwight_x minus 1
fig

leftbutton moveLEFT
--> we are calling the function moveLEFT

In here we are decreasing the value of x position of our character to make him move left .


Now lets see the function for RIGHT button :

function moveRIGHT
task "move right"
dwight_x plus 1
fig

rightbutton moveRIGHT
--> we are calling the funtion moveRIGHT

For the up and down functions we have to change the value of dwight_y the same way .



Lets see the function for growing beets :

function growBeets
task "plant beet" return task
fig

beetbutton growBeets


Like this , we have tasks for dwight which we can give to him as many times we want .

Here is an example of how you can decorate the farm :


YOU CAN USE THE ARROW KEYS TO MOVE DWIGHT TOO. PRESS THE RESPECTIVE UNDO BUTTONS TO REMOVE YOUR BEETS, CARROTS AND HAY

KNOW MORE ABOUT DWIGHT SCHRUTE -- https://en.wikipedia.org/wiki/Dwight_Schrute