Parameters are inputs to a function that allow the function to operate on different values or data sets each time it is called.
When defining a function, you can specify one or more parameters in the function declaration. These parameters act as placeholders for the actual values that will be passed into the function when it is called.
You can have any number of parameters in a function declaration, separated by commas. When you call the function, you can pass in as many arguments as necessary to match the number of parameters in the function definition.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
function with_param(x) {
alert(x);
}
with_param(3423423);
with_param("Something");
</script>
</body>
</html>
This code defines a function named with_param
that takes a single parameter x
.
The function displays an alert box with the value of the x
parameter when it is called using the alert(x)
statement.
The code then calls the with_param
function twice with different arguments: 3423423
and "Something"
.
The first call passes in the number 3423423
as the value for the x
parameter, while the second call passes in the string "Something"
.
When you run this code, you should see two alert boxes pop up in your browser window, each containing the value of the corresponding argument that was passed into the with_param
function.
The first alert box should display the number 3423423
, and the second alert box should display the string "Something"
.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
function with_param(x) {
alert(x);
document.write(x);
document.write("<hr>");
document.write(x);
}
with_param("Something New");
</script>
</body>
</html>
This code defines a function named with_param
that takes a single parameter x
.
The function displays an alert box with the value of the x
parameter using the alert(x)
statement.
It then uses the document.write()
method to write the value of the x
parameter to the web page twice, separated by a horizontal rule (<hr>
).
The code calls the with_param
function once with the string "Something New"
as the argument.
When you run this code, you should see an alert box pop up in your browser window with the message "Something New"
.
Below the alert box, you should see the string "Something New"
written twice to the web page, separated by a horizontal rule.
<!DOCTYPE html>
<html>
<head>
<!-- <script src="main.js"></script> -->
</head>
<body>
<script>
function with_param(x) {
alert(x);
document.write(x);
document.write("<hr>");
document.write(x);
}
function grab_param(z) {
with_param(z);
}
grab_param("Func to Func");
</script>
</body>
</html>
This code defines two functions: with_param
and grab_param
.
The with_param
function takes a single parameter x
and displays an alert box with the value of the x
parameter. It then writes the value of the x
parameter to the web page twice, separated by a horizontal rule (<hr>
).
The grab_param
function takes a single parameter z
. It calls the with_param
function and passes in the z
parameter as an argument.
The code then calls the grab_param
function once with the string "Func to Func"
as the argument.
When you run this code, you should see an alert box pop up in your browser window with the message "Func to Func"
.
Below the alert box, you should see the string "Func to Func"
written twice to the web page, separated by a horizontal rule.
This code demonstrates how one function can call another function and pass in a parameter as an argument. In this example, the grab_param
function calls the with_param
function and passes in the z
parameter as the value for the x
parameter in the with_param
function.
No comments:
Post a Comment