βœ… NEW PRODUCTS
🚚 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