+ 2
Variables vs Paramaters
I cant seem to distinguish a variable vs a paramater can anybody explain the difference between them. People say paramaters are like temporary variables what exactly does that mean?
8 Réponses
+ 6
// An example says more than a thousand words
// a and b are parameters
public int addAmp(int a, int b)
{
int k = 2; // k is a variable
return k*(a + b);
}
// 2 and 3 are arguments (parameter values)
// sum is a variable
var sum = addAmp(2, 3);
+ 3
Made me very happy
+ 2
VARIABLES :
x = 10 # Here, 'x' is a variable that holds the value 10.
name = "John" # 'name' is a variable that holds the string "John".
PARAMETERS :
def add_numbers(a, b):
# 'a' and 'b' are parameters.
return a + b
result = add_numbers(5, 7)
# In this call, 5 and 7 are the arguments passed to the 'add_numbers' function.
# Inside the function, 'a' will be assigned the value 5, and 'b' will be assigned the value 7.
# The function will return 5 + 7 = 12, and that value will be stored in the 'result' variable.
+ 1
Parameters are passed inside a function while variables are declared like INt a=1,string s etc.
+ 1
Thanks
+ 1
I understand now
0
Let’s take a real life example to clarify your question.
I suppose you used MS Excel at some point.
Says in cell “A1”, you enter =SUM(1, 4).
In here the number 1 and 4 are parameters for the SUM function.
Now we add some more. Says in cell “A2” you entered 10, and in cell “A3” you entered 20.
Back to A1, we change the formula to =SUM(A2, A3).
Now A2 and A3 are parameters for function SUM, in the meantime they are also a variable, where A2 represent 10 and A3 represent 20.
Back in coding we say:
X = 10
Y = 20
print(X + Y) PS. It can be console.log, printf or something else, depends on which language you use.
Here, X and Y are variables representing 10 and 20, meanwhile they also are parameters for function print.
0
Haile