Wednesday, April 23, 2025

JavaScript Return Values

A return value is the value that a function or subroutine sends back to the caller. When a function completes its execution, it can optionally return a value, which is then used by the calling code. 

Return values are an important part of programming because they allow functions to be used in a wide variety of contexts, and they allow complex programs to be broken down into smaller, more manageable pieces.


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

<script>

	function addition(x, y) {
		add = x + y;
		return add;
	}
	
	document.write(addition(100, 20));
	document.write("<br><br>");
	
	document.write(addition(25, 75));
	document.write("<br><br>");
	
	document.write(addition(5, 995));
	
</script>
	
</body>
</html>

This code defines a function named addition that takes two parameters: x and y.

The function adds the values of x and y together and stores the result in a variable named add. It then uses the return keyword to send the value of add back to the caller.

The code then calls the addition function three times with different arguments, using the document.write() method to write the return value of each call to the web page, separated by two line breaks (<br><br>).

When you run this code, you should see the return values of the addition function written to the web page, separated by two line breaks. The first call to addition should return 120, the second call should return 100, and the third call should return 1000.

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 .  ...