C Programming - Functions - Discussion

1. 

Point out the error in the program


f(int a, int b)
{
    int a;
    a = 20;
    return a;
}

[A]. Missing parenthesis in return statement
[B]. The function should be defined as int f(int a, int b)
[C]. Redeclaration of a
[D]. None of above

Answer: Option C

Explanation:

f(int a, int b) The variable a is declared in the function argument statement.

int a; Here again we are declaring the variable a. Hence it shows the error "Redeclaration of a"


Sonia said: (Jul 4, 2011)  
Can anyone explain me this?

Manishsoni said: (Sep 22, 2011)  
We can't re-declare any variable in the same block.

Abc said: (Nov 6, 2011)  
Even return type is also missing rite?

Anmol said: (Dec 12, 2011)  
The default return type of any function is "int", so it is useless to write
"int f(int a, int b)".

Khushboo said: (Apr 9, 2012)  
At many places we see void as default return type of function for example in declaration of main we only write main ()
{} and do not return any value so anyone please explain this confusion. ?

Hakkim said: (Jun 4, 2012)  
@ Khushboo
Your are right, in C RETURN TYPE IS NOT NEEDED when we write FUNCTION DEFINITION, it is needed only at the FUNCTION DECLARATION.
So for this program Re-Declaration of variable 'a' is the only problem.

For example refer the below program which doesn't have return type at definition.

#include<stdio.h>
int add(int,int);
int main()
{
int a,b,c;
printf("Enter two numbers :");
scanf("%d" "%d",&a,&b);
c = add(a,b);
printf("Addition Value = %d",c);

return 0;
}
add(int x, int y)
{
int z;
z = x + y;
return z;
}

Ganesh said: (Jun 6, 2012)  
@ Khushboo and @ Hakkim when I am running program without mentioning the return type of main () and I didn't return anything then it is giving an warning that "function should return a value. " so default return type of main () should be int. Please give your responce.

S.Nandhini said: (Jul 5, 2012)  
Even if we use return in the program, instead of using getch() ; the program doesn't run. Can anyone clear my doubt ?

Ashish said: (Sep 2, 2012)  
int a; //declaration part
a=20; //defination part
so why its give error?
please explian...

Rosmy said: (May 14, 2013)  
@Ashish:

f(int a,int b)// a is declared as int.
{
int a; // error: Redeclaration.
...
}

Sayli said: (Jul 17, 2016)  
Yes, I totally agree with you @Rosmy.

Deepali Bajaj said: (Mar 25, 2018)  
The return type is not declared.

Mayuu said: (Oct 16, 2018)  
@Ganesh yeah!

True. The default return type may be void.

Harishmahajan said: (Oct 16, 2018)  
How redeclaration of a?

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.