-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V1.13.5: Added Loops and Functions to docs and updated README
- Loading branch information
Showing
9 changed files
with
173 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Functions | ||
|
||
## What are functions? | ||
|
||
Functions are like a mini-program, that you can call in your main program. | ||
|
||
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. | ||
|
||
## Defining Functions in NumberScript | ||
|
||
now to define functions we need the `7` command. | ||
|
||
so the basic syntax is this | ||
|
||
``` | ||
7function-name$function-arguments$function-body | ||
``` | ||
|
||
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. | ||
|
||
so here is a function that adds two numbers and prints them. | ||
|
||
``` | ||
7add$number1,number2$2^number1+number2 | ||
``` | ||
|
||
now how do I call this function well just do this | ||
|
||
``` | ||
add$5,5 | ||
``` | ||
|
||
and it will print 10. | ||
|
||
so how can I have more then one command in the function body? | ||
well simply you can add the function code split in between the commands. | ||
the function split is the `$`. | ||
|
||
so here is our function that prints the sum of two numbers. but this function is different. | ||
I changed it to display the double of the sum. | ||
|
||
``` | ||
7add$number1,number2$3sum:^number1+number2$2^sum*2 | ||
``` | ||
|
||
so if we call this with `add$5,5` it will print 20. | ||
|
||
and that's about it for functions. | ||
|
||
## Exercises | ||
|
||
Now that you got the basics of the functions. try these exercises. | ||
|
||
### 1) fisherman's problem | ||
|
||
now A fisherman has a brand new programmable fishing rod. now the rod is programmed with NumberScript. | ||
so now the fisherman asks you to program the rod. since he is a fisherman he doesn't know how to program. | ||
|
||
so he asks you to make a program to take input. the options are `1` for `small fish` and `2` for `big fish`. | ||
|
||
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. | ||
|
||
now the calculation on how to pull the small fish is `weight/2 + 1`. | ||
|
||
for big fish it's `weight * 2 / 4 + 1`. | ||
|
||
so here is an example input and output. | ||
|
||
``` | ||
Enter-Option>1 | ||
Enter-Weight>4 | ||
Pull-the-fish-with, | ||
3 | ||
grams | ||
``` | ||
|
||
``` | ||
Enter-Option>2 | ||
Enter-Weight>10 | ||
Pull-the-fish-with, | ||
6 | ||
grams | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Loops | ||
|
||
## What are loops? | ||
|
||
now what loops do is repeat an instruction a number of time. | ||
|
||
now imagine you have an instruction you have to do a 100 times. | ||
you can write it 100 times. but that's not efficient. and it's not fun. | ||
so instead you can use loops. | ||
|
||
now in NumberScript there is only 1 type of loop. which is the for loop. | ||
|
||
## Loops in NumberScript | ||
|
||
now the syntax for loops is this | ||
|
||
``` | ||
6loop-variable\repeat-count\loop-code | ||
``` | ||
|
||
now the loop-variable is the variable that will be used to count the number of times the loop has been repeated. | ||
the repeat count is the times it will repeat. the loop code is the instruction that will be repeated. | ||
|
||
now let's see an example | ||
|
||
``` | ||
6i\10\2i | ||
``` | ||
|
||
here this will print the numbers from 0 to 9. | ||
|
||
> Loops start from 0 so the first number will be 0. and the end number will be one less then the placed amount. | ||
now let's see another example | ||
|
||
``` | ||
6i\10\?i=5:2i|2i-is-5 | ||
``` | ||
|
||
here this will print the numbers from 0 to 9. but if the number is 5 it will print `i-is-5` instead. | ||
|
||
## Exercises | ||
|
||
### 1) Odd or Even | ||
|
||
now, we made a program to see if the number is even or odd in the [If-Else](NumberScript/Tutorial/if-else/#even-or-odd) section. but now your task is to make it so the program loops through the numbers from 0 to 99 and prints if the number is even or odd. | ||
|
||
here is the output. | ||
|
||
``` | ||
0 | ||
is even | ||
1 | ||
is odd | ||
2 | ||
is even | ||
3 | ||
is odd | ||
... | ||
``` | ||
|
||
### 2) FizzBuzz | ||
|
||
Now your task is to create a FizzBuzz program where it loops over the numbers from 1 to 100 and prints `Fizz` if the number is divisible by 3, `Buzz` if the number is divisible by 5, `FizzBuzz` if the number is divisible by 3 and 5, and the number if it's not divisible by 3 or 5. | ||
|
||
here is the output. | ||
|
||
``` | ||
1 | ||
2 | ||
Fizz | ||
4 | ||
Buzz | ||
Fizz | ||
7 | ||
8 | ||
... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# About | ||
|
||
Created by [Sas2k](https://github.com/Sas2k) | ||
Created by [Sas2k](https://github.com/Sas2k) on a rainy day. | ||
|
||
> Also some of you might have noticed that this language works with only string parsing. | ||
> No Lexers, Parsers or anything of that sort. :smile: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters