UNDERSTANDING FUNCTIONS

           
[h1]WHAT ARE FUNCTIONS ?[h1] Functions enhance the re usability of code . You can keep your code in a function and use it several times . For example : *[c12]function hi[c12] 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 [c12]hi[c12] * 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 : *[c12]function hi message [c12] now message print fig var [c12]hi[c12] "Hello Pam" var [c12]hi[c12] "Hello Jim" OUTPUT: Hello Pam Hello Jim * You can use return command in functions to return a value like this : *[c12]function hi name [c12] now "hello" plus name return now fig var [c12]hi[c12] "Pam" print OUTPUT: hello Pam * The function will return the value of now variable [h1]UNDERSTANDING WITH GAME![h1] *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: *[c12]function moveLEFT [c12] task "move left" dwight_x minus 1 fig leftbutton [c12]moveLEFT[c12]*--> 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 : *[c12]function moveRIGHT[c12] task "move right" dwight_x plus 1 fig rightbutton [c12]moveRIGHT[c12] * --> 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 : *[c12]function growBeets[c12] task "plant beet" return task fig beetbutton [c12]growBeets[c12] * 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 : [img]dwiex.png[img] *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 -- [url]https://en.wikipedia.org/wiki/Dwight_Schrute[url]