A question about functions' arguments

Hello Every Body,

I have a question regarding a code that I am reading,
Here are the definitions of some functions:

1
2
3
void SetTargetLength (double  val)      {length =val};
void SetNumberofSlices(int val)         {nSlices=val};
void SetNumberofBins(int val)           {nBinsE=val};


I am wondering by this declaration length and nSlices and nBinsE should take the same value???

But in another place in my code I see that they have been initialized like this:

1
2
3
nSlicces=300;
nBinsE=100;
length=300.*mm;


Many thanks in advance

Assuming that length, nSlices and nBinsE ar global variables, their values are set by the arguments of their specific functions. What this means is that when you call SetTargetLength, the global variable length will be set to the argument you called SetTargetLength with. For example, if you call SetTargetLength(5.1), length will be set to 5.1. However, nSlices and nBinsE will not be set to 5.1, because they weren't called with that value.

The arguments of a function are local to that function.
In other words, what toexii is saying is the variable "val" is not essentially the same, each has a life time that ends when "}" of the function is encountered, meaning when the function call ends it ceases to exist.

length and nSlices and nBinsE would take the same value if "val" was not provided as an argument, but rather declared outside the function

hope that helps further
Topic archived. No new replies allowed.