parameters of a function in C

if say I have :

can I do such thing in C? Not in C++ or C#
int foo(int a, int b){
  a = 2;   ============> is this possible?
  return a; ===========> is this possible?
}

Open in new window

kuntilanakAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Frosty555Commented:
I don't see anything wrong with the code you posted. Parameters aren't really anything special. They're just variables that get pre-filled by the caller just before the function runs.

Keep in mind that the variable "a" will be passed by value from the caller. As a result, changing it's value to 2 will not cause anything to happen to the variables on the caller's side.
0
jfmadorCommented:
you should use const to avoid modifying a parameter

int foo(const int a, int b) {
   a = 2;  // should not compile
}

but if you don't use const you should be able to modify the value

for the return a;  I don't see why it could not possible
0
kuntilanakAuthor Commented:
so you're saying that return a; although I assigned a = 2 won't give me anything?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Frosty555Commented:
What jfmador is suggesting is a good programming practice - not mandatory.

Your code will work fine, but it is bad programming style to modify a paramter. In theory it should have no impact because the parameter is passed by value, but if you get into the habit of directly modifying parameters, one day you'll write a program, and the parameter will be passed by REFERENCE, and you'll cause bugs you will spend hours trying to fix.

Putting "const" into the parameter forces C to fail with a compile error if you attempt to modify the parameter's value, thereby forcing you to follow this good programming style.

But, syntactically, I don't think there's anything wrong with what you're doing.
0
sunnycoderCommented:
return a would return value 2
0
Anurag ThakurTechnical ManagerCommented:
i dont see anything wrong in you function in C or in C#
you are passing a and b as parameters and they are being passed as normal stack variables and not as references, even if you assign 2 to a and then return it will pose no issues with the compiler and logic.
if you are expecting that the value of the passed variable a changes in the caller function then you might need to use a ref or out (in C#) or use pointers in C

int foo(int a, int b)
{
  a = 2;   ============> is this possible?
  return a; ===========> is this possible?
}
0
kuntilanakAuthor Commented:
okay.. all I need to know here is just whether this kind of statement is allowed or not.. some statements in C are tricky... having int a in a parameter, is it basically the same as declaring a variable inside the function body?
0
sunnycoderCommented:
Yes it is allowed
0
Frosty555Commented:
Yes. the parameter is no different from any other variables in the function. The only difference is that it's value is filled for you.
0
kuntilanakAuthor Commented:
ok, if that's the case then doing something like:

will cause a multiple declaration of identifier right?
int foo(int a, int a, int b){
  a = 2;   
  return a; 
}

Open in new window

0
Frosty555Commented:
the compiler should complain that you have two variables named "a", and that is not allowed.

Similarey this is not allowed and should fail at compile time, because "a" has already been defined.
int foo(int a, int b){
  int a = 2;
  return a; 
}

Open in new window

0
sunnycoderCommented:
Yes it would. How would compiler know which a to use?
0
Anurag ThakurTechnical ManagerCommented:
the code in ID:22773540 will not compile
the variables have to have different names at least
0
kuntilanakAuthor Commented:
how about in function definition:

int foo(int a, int a);
0
Anurag ThakurTechnical ManagerCommented:
no i will not
try it in your compiler
how can two variables have same name in the same scope.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kuntilanakAuthor Commented:
they are just a definition though :), not the actual function declaration...
0
sunnycoderCommented:
int foo(int , int );
would compile
int foo(int a, int a); wont
0
kuntilanakAuthor Commented:
ok, thanks all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.