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