Wednesday, April 23, 2025

JavaScript Functions

A function is a block of code that performs a specific task and can be reused multiple times throughout a program. Functions allow you to write code once and use it many times, which can help reduce redundancy and make your code more efficient.

Here is the basic syntax for creating a function in JavaScript: 

function functionName(parameters) {
  // code to be executed
}
  • function: The keyword that tells JavaScript you are creating a function.
  • functionName: The name of the function. This can be any valid identifier.
  • parameters: The inputs to the function. These are optional, and you can have multiple parameters separated by commas.
  • code to be executed: The code that the function will execute when called.

Here is an example of a simple function that takes two parameters and returns their sum: 

function addNumbers(num1, num2) {
  return num1 + num2;
}

To call this function, you would use the function name followed by the arguments (values) you want to pass in: 

var result = addNumbers(2, 3);
console.log(result); // Output: 5

This would call the addNumbers function with arguments 2 and 3, and store the result in the result variable. The function would then return the sum of the two numbers, which would be 5.

Functions are a fundamental concept in JavaScript, and they are used extensively in web development to perform a variety of tasks, from simple calculations to complex data processing and manipulation. They are an essential part of the language and one of the reasons why JavaScript is so powerful and flexible. 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function some_func() {
		alert("I am from Func");
	}
	
	function run_3_times() {
		some_func();
		some_func();
		some_func();
	}
	
	run_3_times();
	
</script>
	
</body>
</html>

This code defines two functions: some_func and run_3_times.

The some_func function simply displays an alert box with the message "I am from Func".

The run_3_times function calls the some_func function three times in a row using the some_func(); statement three times.

Finally, the run_3_times(); statement calls the run_3_times function, which in turn calls the some_func function three times.

When you run this code, you should see three alert boxes pop up in your browser window, each containing the message "I am from Func". 


<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>

<script>

	function some_func() {
		alert("I am from Func");
	}
	
</script>

<input type="button" value="Click Me"
onclick="some_func()">
	
</body>
</html>

This code defines a function named some_func that displays an alert box with the message "I am from Func".

It also creates an HTML input button with the label "Click Me". When this button is clicked, the some_func function is called using the onclick event handler.

The onclick event handler is a built-in JavaScript event that is triggered when the button is clicked. In this case, it calls the some_func function using the some_func() statement.

When you load this code in a browser and click the "Click Me" button, you should see an alert box pop up with the message "I am from Func".

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...