๐ŸŽ TENYO 2026
๐Ÿšš Shipping from 12 USD
๐Ÿ’ฐ DEALS OF THE DAY

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

โš›๏ธ Atom Remote

๐Ÿ”ข Quantum Calculator

๐Ÿคนโ€โ™‚๏ธ MagiScript Language

๐Ÿคนโ€โ™‚๏ธ MagiScript Examples

๐Ÿคนโ€โ™‚๏ธ MagiScript Tutorials

General

๐Ÿ“ƒ First Steps

MagiScript Editor

๐Ÿ“ƒ Basics

๐Ÿ“ƒ Keyboard Shortcuts

๐Ÿ“ƒ Running Your First Program

๐Ÿ“ƒ App Store

๐Ÿ“ƒ Atom Settings (Editor)

๐Ÿ“ƒ 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

๐Ÿ“ƒ Variable Declarations

๐Ÿ“ƒ Operators

๐Ÿ“ƒ Control Flow

๐Ÿ“ƒ Functions

๐Ÿ“ƒ Numbers

๐Ÿ“ƒ Strings

๐Ÿ“ƒ Arrays

๐Ÿ“ƒ Objects

๐Ÿ“ƒ Uint8Array