sexta-feira, 22 de novembro de 2019

Squaring a number

Hello, MiniScripters! Today you'll learn about lists, "if"s and more.
Let's make a program that squares any number the user wants!
It's a very simple program, but you can also make better things from this.


With MiniMicro

Step 1


Type:
f = function(x)

Explaining "f = function"
"f" is the name of the function, and you can put any other name you want. So "f = function" defines that there's a function called "f". That "(x)" is just a variable (or parameter) that the function uses.


Step 2


Type:
   return x^2

Explaining "return x^2"
The "return" command defines the value of the function. "x^2" print the square of x


Step 3


Now just type:
end function

...but now, instead of  type "run", you must type:
f  2 (typing "f", you call the function "f", that prints the square of any number.)

If you type "f 2", the program must print 4. If you type "f 3", it must print 9. Now we have a function that prints the square of a number.
Well, it's just it for now. It's a small post, but now you know the basic to make a simple function.

With Try It!

Again, with Try It! you must to type exactly the same code, except that in Try It!, you must to change slightly the code. Write this:

...run the program and you must to see this:


I must explain somethings about MiniMicro and Try It!. When you write in the MiniMicro's command prompt, you are in a REPL (Read Eval Print Loop). A REPL means that the computer
Read what you type, Evaluate it and Print the result -- and Loop this.
      But if you are using Try It! you are not in a command prompt. The same if you are editing a program in MiniMicro(with the `edit` command). In this case, you are writing a program, not using a REPL.
So if you are writing a program, you must to write "print f(2)", and if you are in a command prompt, you can type just "f 2".


Well, this is the end of the post, see you later!

Nenhum comentário:

Postar um comentário