Im fairly new to c# programming. What does passing a parameter mean? and can someone give me an example please?
Thank you.
Im fairly new to c# programming. What does passing a parameter mean? and can someone give me an example please?
Thank you.
Functions in languages such as C# (and many, many, others) can take "parameters". These are things you pass in to let the function do whatever it was designed to do. Consider:
public int Add(int x, int y)
{
return x + y;
}
int a = 5;
int b = 5;
int sum = Add(a, b);
In the above example, we are passing the variables a
and b
to the function Add
. The function takes two parameters, x
and y
, and adds them together returning the result.
In simple sense,passing a parameter means to give an input to the function. let me give you an example
below is the algorithm for adding two numbers and printing the result
void add(int a,int b)
{
int sum
sum=a+b
print sum
}
So we have defined a function. The function add contains two arguments. An argument is a 'holder' for the values ie a variable.Now it expects two parameters( input) a and b.
Hence to use this function we must call it like this
add(1,2)
the numbers inside the paranthesis are called parameters. they are input for your function