Solved

parameters of a function in C

Posted on 2008-10-21
18
184 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:kuntilanak
  • 6
  • 4
  • 4
  • +2
18 Comments
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773448
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
 
LVL 5

Expert Comment

by:jfmador
ID: 22773459
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
 

Author Comment

by:kuntilanak
ID: 22773478
so you're saying that return a; although I assigned a = 2 won't give me anything?
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 31

Expert Comment

by:Frosty555
ID: 22773493
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22773494
return a would return value 2
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22773511
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
 

Author Comment

by:kuntilanak
ID: 22773523
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22773528
Yes it is allowed
0
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773531
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
 

Author Comment

by:kuntilanak
ID: 22773540
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
 
LVL 31

Expert Comment

by:Frosty555
ID: 22773548
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22773552
Yes it would. How would compiler know which a to use?
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 22773560
the code in ID:22773540 will not compile
the variables have to have different names at least
0
 

Author Comment

by:kuntilanak
ID: 22773563
how about in function definition:

int foo(int a, int a);
0
 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 500 total points
ID: 22773586
no i will not
try it in your compiler
how can two variables have same name in the same scope.
0
 

Author Comment

by:kuntilanak
ID: 22773591
they are just a definition though :), not the actual function declaration...
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22773632
int foo(int , int );
would compile
int foo(int a, int a); wont
0
 

Author Comment

by:kuntilanak
ID: 22773655
ok, thanks all
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Close form "before" open 3 40
Code works but it's slow 28 70
SqlDataBase 7 47
updating order of the rows, N+1 query issue, C#, asp.net, MVC, SQL Server 3 25
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question