Functions
Basics
Functions are a fundamental concept in programming languages, including MagiScript. A function is a block of code that performs a specific task and can be called from anywhere in the program. Functions help in writing reusable code and make the code more organized and modular.
In MagiScript, there are two ways to define a function: using theΒ functionΒ keyword and using arrow functions.
Function Keyword
Using theΒ functionΒ Keyword: TheΒ functionΒ keyword is used to define a named function in MagiScript. The syntax for defining a function is as follows:
function functionName(parameter1, parameter2, ... parameterN) {
// function code
}
Here,Β functionNameΒ is the name of the function, andΒ parameter1,Β parameter2, …Β parameterNΒ are the parameters passed to the function. The function code is enclosed within curly bracesΒ {}.
For example, let’s define a function that adds two numbers and returns the result:
function addNumbers(num1, num2) {
return num1 + num2;
}
console.log(addNumbers(3, 4));
Here,Β addNumbersΒ is the name of the function, andΒ num1Β andΒ num2Β are the parameters passed to the function. The function code adds the two parameters and returns the result using theΒ returnΒ keyword.
Arrow Functions
Arrow functions are a shorthand way to define a function in JavaScript, and it is supported by MagiScript as well. They are also known as “fat arrow” functions. The syntax for an arrow function is as follows:
(parameter1, parameter2, ... parameterN) => {
// function code
}
Here,Β parameter1,Β parameter2, …Β parameterNΒ are the parameters passed to the function, and the function code is enclosed within curly bracesΒ {}. TheΒ =>Β operator separates the parameters and the function code.
For example, let’s define an arrow function that multiplies two numbers and returns the result:
const multiplyNumbers = (num1, num2) => {
return num1 * num2;
}
console.log(multiplyNumbers(111, 6));
Here,Β multiplyNumbersΒ is the name of the arrow function, andΒ num1Β andΒ num2Β are the parameters passed to the function. The function code multiplies the two parameters and returns the result using theΒ returnΒ keyword.
Calling a Funktion
Once a function is defined, it can be called from anywhere in the program. To call a function, use the function name followed by parenthesesΒ ()Β and pass the arguments, if any, inside the parentheses.
For example, to call theΒ addNumbersΒ function defined above, we can write:
let result = addNumbers(10, 20);
console.log(result); // Output: 30
Here, we passΒ 10Β andΒ 20Β as arguments to theΒ addNumbersΒ function, which adds them and returnsΒ 30. We store the result in theΒ resultΒ variable and log it to the console using theΒ console.log()Β function.
Conclusion
Functions will help you to write modular and reusable code. They can be defined using the function keyword or arrow functions and can be called from anywhere in the program. By using functions, you can write code that is more organized, easier to maintain, and more efficient.
Devices & MagiScript
π€ΉββοΈ MagiScript Language
π€ΉββοΈ MagiScript Examples
π€ΉββοΈ MagiScript Tutorials
General
π First Steps
MagiScript Editor
π Basics
π Keyboard Shortcuts
π Running Your First Program
π App Store
π Debugging Techniques
Input/Output
π Buttons
π Vibration Motor
π RGB LED
π Devices
π PeekSmith
π Quantum
π Teleport
π Spotted Dice
π SB Watch
π IARVEL Watch
π Fossil Watch
π Cosmos Printer
π PeriPage Printer
π ATC Remote
π Labco Scrabble
π Bluetooth Keyboard
π Bluetooth Mouse
π Timers
π Database
π Events
π System (exit, sleep, rand)
π Objects (card, time)
Language
π Summary
π Comments
π Operators
π Control Flow
π Functions
π Numbers
π Strings
π Arrays
π Objects
π Uint8Array