Passing parameters to a function [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can b开发者_C百科e answered with facts and citations.
Closed 7 years ago.
Improve this questionI am new to JavaScript. I am learning it but I am not understanding the "function with parameters". Why a function require a "parameters" & how would we know that this function will require a "parameters". Can you provide me with a link where I can learn all the JavaScript from basic & I want a link where all small JavaScript programs with details are provided.
A function would require parameters if its output is based on some input values. For instance, if you were going to write a function that adds two numbers together:
function adder(a, b) {
return a + b;
}
I know that this function takes two parameters named a
and b
because I can see it in the definition after the function name: adder(a, b)
. I need to "pass" two numbers to this function for it to produce good output, like so:
adder(2, 3); // returns 5
If I don't pass it two numbers, the returned result will be NaN
, because the function's arguments will be undefined
:
adder(); // a = undefined, b = undefined, returns NaN
adder(7); // a = 7, b = undefined, returns NaN
For more information on learning Javascript, one place to go is the MDN guide.
A function does not require paremeters. All functions are made of 2 essentials parts:
- The Implementation - Which is the actual code of the function, what it does.
- The Interface - The things that connect it to the "outside world" aka, the rest of the program.
Parameters are part of the interface, you can pass parameters to your function in order to help it achieve its goal towards the overall program.
Consider the following (theoretical) example:
Let's say I want to make a TV operation program. I want to define a way to switch channels.
function switchChannel() { /* do some stuff */ }
However, what would be the first thing that you would ask yourself? What channel does the user want to change to? That's the interface problem, so in order to include that in our function, we would do something like this:
function switchChannel(channelNum) { /* do some stuff */ }
When I call the function, I will most likely do it like so:
switchChannel(24);
That 24 value will be passed to the channelNum
parameter inside of the switchChannel()
function.
So the complete example code:
function switchChannel(channelNum) { alert(channelNum); }
switchChannel(24);
The result would be an alert with 24
That's the gist of it, hope it was helpful for you :)
I recommend to view this page http://www.w3schools.com/js/js_functions.asp.
And I will add few words in my way.
For example we want to change background color of the page
for that I will create function like this
function changeBG(whichColor){
document.bgColor = whichColor;
}
So, I can call the function from button click (Red button) -- onclick="changeBG('red');"
same and I can call that method with diff parameter (for example onclick="changeBG('green');"
we can reuse the same code, when we need it.
This is not specific to javascript.
All programming languages have functions which require parameters.
Actually, all functions have parameters. A function is just like you learn about in math class: you give it an input value, and it gives you an output value.
A function with "no parameters" actually has 0 parameters. By its nature, it can only return one value (unless it's accessing external variables, which is very poor form).
For example:
function sum0() { // <-- "no" parameters/arguments
return 0;
}
function sum1(x0) { // <-- 1 parameters/arguments
return x0;
}
function sum2(x0, x1) { // <-- 2 parameters/arguments
return x0+x1;
}
function sum3(x0, x1, x2) { // <-- 3 parameters/arguments
return x0+x1+x2
}
(Of course you would never write these functions; they're terrible and repetitive. The entire reason we write functions it to avoid having to repeat things. To actually implement a sum function, you'd either pass in an array as a single argument, or do return arguments.reduce(sum2)
)
A function which has no parameters is sometimes called a "thunk", because it doesn't need to process new information. It's "already thought-out". For example:
function firstNPrimes(n) { // 1 parameter, not "thought out"
// sieve of Eratosthanes goes here
}
Say we now wanted a function firstFivePrimes
. We have many ways we could do so:
function firstFivePrimes() { // no parameters, already "thought out"
return firstNPrimes(5);
}
However it's already "thought-out", so we could just pre-calculate it, or calculate it in the background:
function firstFivePrimes() { // no parameters, already "thought out"
return [2,3,5,7,11];
}
The two implementations are identical, assuming you are not accessing global variables (poor form).
Here's a "thunk" which always returns the same value: a function!
function makeCounter() {
var i = 0;
return function() {
console.log(i++);
}
}
Example:
> counter = makeCounter()
> counter()
0
> counter()
1
> counter()
2
> anotherCounter = makeCounter()
> anotherCounter()
0
> anotherCounter()
1
> counter()
3
> anotherCounter()
2
精彩评论