+ 1
Function Parameters
what exactly it is meant by function parameters
2 Answers
+ 3
Parameters are values or objects that can be passed to the function that can be altered in the function and also used by the function to obtain a desired result.
Ex:
function multiply(int a, int b) {
var result = a * b;
return result;
}
var c = multiply(3, 7);
// c now contains the integer return value from the
// multiply function
// which multiplies parameters a and b (3 and 7)
// giving it the value 21
+ 3
Function parameters are data required that you give to the function. For example, if a function called Add() needs two numbers to add, you would define the function as Add(int x, int y), where x and y are the parameters (in this case, they're integers). So you would need to give it 2 integers, which it adds together.
int Add( int x, int y) {
return x + y;
}
In the example, the function Add() adds the x and y parameters and returns them. You would use it like this:
int sum = Add(10, 5);
here, sum would be equal to 15.
(not sure how you would write it in JS, I use C++, but the concept is definitely the same).
Of course, this example function is useless because all modern languages have built in operators that you can use instead (such as the + and - operators that add and subtract), but nonetheless I think it's a clear example.
I currently use C++ to program 3D graphics. I made a function called setLocation(x,y,z). When called, it sets the location of a 3D model (be it a house or a lamp) to that x,y,z location. An example of a call would be setLocation(10,5,11).