C Programming - Functions - Discussion
Discussion Forum : Functions - Point Out Errors (Q.No. 1)
1.
Point out the error in the program
f(int a, int b)
{
int a;
a = 20;
return a;
}
Answer: Option
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"
Discussion:
14 comments Page 1 of 2.
Hakkim said:
1 decade ago
@ 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;
}
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;
}
(1)
Ganesh said:
1 decade ago
@ 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.
Khushboo said:
1 decade ago
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. ?
{} and do not return any value so anyone please explain this confusion. ?
Anmol said:
1 decade ago
The default return type of any function is "int", so it is useless to write
"int f(int a, int b)".
"int f(int a, int b)".
S.Nandhini said:
1 decade ago
Even if we use return in the program, instead of using getch() ; the program doesn't run. Can anyone clear my doubt ?
Ashish said:
1 decade ago
int a; //declaration part
a=20; //defination part
so why its give error?
please explian...
a=20; //defination part
so why its give error?
please explian...
Rosmy said:
1 decade ago
@Ashish:
f(int a,int b)// a is declared as int.
{
int a; // error: Redeclaration.
...
}
f(int a,int b)// a is declared as int.
{
int a; // error: Redeclaration.
...
}
Mayuu said:
7 years ago
@Ganesh yeah!
True. The default return type may be void.
True. The default return type may be void.
Manishsoni said:
1 decade ago
We can't re-declare any variable in the same block.
Abc said:
1 decade ago
Even return type is also missing rite?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers