|
| 1 | +# Functions |
| 2 | + |
| 3 | +## What are functions? |
| 4 | + |
| 5 | +Functions are like a mini-program, that you can call in your main program. |
| 6 | + |
| 7 | +so like in some text editors, let's take word for example. there is a button in the top corner that makes the text bold. this is a function. like this there a lot of functions in Word. |
| 8 | + |
| 9 | +## Defining Functions in NumberScript |
| 10 | + |
| 11 | +now to define functions we need the `7` command. |
| 12 | + |
| 13 | +so the basic syntax is this |
| 14 | + |
| 15 | +``` |
| 16 | +7function-name$function-arguments$function-body |
| 17 | +``` |
| 18 | + |
| 19 | +now the function-name is the name of the function. the function-arguments are the arguments that the function takes. and the function-body is the code that the function runs. |
| 20 | + |
| 21 | +so here is a function that adds two numbers and prints them. |
| 22 | + |
| 23 | +``` |
| 24 | +7add$number1,number2$2^number1+number2 |
| 25 | +``` |
| 26 | + |
| 27 | +now how do I call this function well just do this |
| 28 | + |
| 29 | +``` |
| 30 | +add$5,5 |
| 31 | +``` |
| 32 | + |
| 33 | +and it will print 10. |
| 34 | + |
| 35 | +so how can I have more then one command in the function body? |
| 36 | +well simply you can add the function code split in between the commands. |
| 37 | +the function split is the `$`. |
| 38 | + |
| 39 | +so here is our function that prints the sum of two numbers. but this function is different. |
| 40 | +I changed it to display the double of the sum. |
| 41 | + |
| 42 | +``` |
| 43 | +7add$number1,number2$3sum:^number1+number2$2^sum*2 |
| 44 | +``` |
| 45 | + |
| 46 | +so if we call this with `add$5,5` it will print 20. |
| 47 | + |
| 48 | +and that's about it for functions. |
| 49 | + |
| 50 | +## Exercises |
| 51 | + |
| 52 | +Now that you got the basics of the functions. try these exercises. |
| 53 | + |
| 54 | +### 1) fisherman's problem |
| 55 | + |
| 56 | +now A fisherman has a brand new programmable fishing rod. now the rod is programmed with NumberScript. |
| 57 | +so now the fisherman asks you to program the rod. since he is a fisherman he doesn't know how to program. |
| 58 | + |
| 59 | +so he asks you to make a program to take input. the options are `1` for `small fish` and `2` for `big fish`. |
| 60 | + |
| 61 | +now when the fisherman sees a small fish he input the option (1 for small fish, 2 for big fish) and the rod will ask him to input the weight of the fish. then the rod will display how hard to pull the line. |
| 62 | + |
| 63 | +now the calculation on how to pull the small fish is `weight/2 + 1`. |
| 64 | + |
| 65 | +for big fish it's `weight * 2 / 4 + 1`. |
| 66 | + |
| 67 | +so here is an example input and output. |
| 68 | + |
| 69 | +``` |
| 70 | +Enter-Option>1 |
| 71 | +Enter-Weight>4 |
| 72 | +Pull-the-fish-with, |
| 73 | +3 |
| 74 | +grams |
| 75 | +``` |
| 76 | + |
| 77 | +``` |
| 78 | +Enter-Option>2 |
| 79 | +Enter-Weight>10 |
| 80 | +Pull-the-fish-with, |
| 81 | +6 |
| 82 | +grams |
| 83 | +``` |
0 commit comments