C, Function search engine

Custom Search

What is declaration of function in C programming?

1.If function definition has written after the function call then it is necessary to declare the function before the function call because function call statement has no idea about prototype of calling function.
Examlple:
float sachin(int x){
    float r=(float)x;
    return r;
}
void main(){
    float f;
    f=sachin(33);
    clrscr();
    printf("%f",f);
    getch();
}
Output : 33.000000
void main(){
    float f;
    f=sachin(33);
    clrscr();
    printf("%f",f);
    getch();
}
float sachin(int x){
    float r=(float)x;
    return r;
}
Output : Compilation error
float sachin(int);
void main(){
    float f;
    f=sachin(33);
    clrscr();
    printf("%f",f);
    getch();
}
float sachin(int x){
    float r=(float)x;
    return r;
}
Output: 33.000000
2.If function definition has written before the function call statement then it is not necessary to write function declaration.
3.If return type of function is signed int data type then it not necessary to write function declaration even though function definition has written after the function call.
4.Function’s declaration doesn’t reserve any memory space.
5.In declaration statement it is not necessary to write variable name in parameter of function.
typedef float klpd(int,char);
void main(){
    float num,num1,num2;
    klpd a,b;
    num1=a(5,'a');
    num2=b(6,'0');
    num=num1+num2;
    clrscr();
    printf("%f",num);
    getch();
}
float a(int x, char y){
    x=x+y;
    return (float)x;
}
float b(int p,char q){
    p=q-p;
    return p;
}
Output: 144.000000
void main(){
    float num,num1,num2;
    num1=a(5,'a');
    num2=b(6,'0');
    num=num1+num2;
    clrscr();
    printf("%f",num);
    getch();
}
float a(int x, char y){
    x=x+y;
    return (float)x;
}
float b(int p,char q){
    p=q-p;
    return p;
}
Output: Compilation error

Parameter passing convention in c

1.pascal: In this style function name should (not necessary ) in the uppercase .First parameter of function call is passed to the first parameter of function definition and so on.
2.cdecl: In this style function name can be both in the upper case or lower case. First parameter of function call is passed to the last parameter of function definition. It is default parameter passing convention.
Examples:
1.What will be output of following program?
void main(){
static int a=25;
void cdecl conv1() ;
void pascal conv2();
conv1(a);
conv2(a);
getch();
}
void cdecl conv1(int a,int b)
{
printf("%d %d",a,b);
}
void pascal conv2(int a,int b)
{
printf("\n%d %d",a,b);
}

Output:
25 0
0 25

(2) What will be output of following program?
void cdecl fun1(int,int);
void pascal fun2(int,int);
void main(){
    int a=5,b=5;
    clrscr();
    fun1(a,++a);
    fun2(b,++b);
    getch();
}
void cdecl fun1(int p,int q){
    printf("cdecl:  %d %d \n",p,q);
}
void pascal fun2(int p,int q){
    printf("pascal: %d %d",p,q);
}
Output:
cdecl:  6 6
pascal: 5 6
(3) What will be output of following program?


void cdecl fun1(int,int);
void pascal fun2(int,int);
void main(){
    int a=5,b=5;
    clrscr();
    fun1(a,++a);
    fun2(b,++b);
    getch();
}
void cdecl fun1(int p,int q){
    printf("cdecl:  %d %d \n",p,q);
}
void pascal fun2(int p,int q){
    printf("pascal: %d %d",p,q);
}
Output:
cdecl:  6 6
pascal: 5 6
(4) What will be output of following program?


void convention(int,int,int);
void main(){
    int a=5;
    clrscr();
    convention(a,++a,a++);
    getch();
}
void  convention(int p,int q,int r){
    printf("%d %d %d",p,q,r);
}
Output: 7 7 5
(5) What will be output of following program?


void pascal convention(int,int,int);
void main(){
    int a=5;
    clrscr();
    convention(a,++a,a++);
    getch();
}
void pascal  convention(int p,int q,int r){
    printf("%d %d %d",p,q,r);
}
Output: 5 6 6
(6) What will be output of following program?


void pascal convention(int,int);
void main(){
    int a=1;
    clrscr();
    convention(a,++a);
    getch();
}
void pascal  convention(int a,int b){
    printf("%d %d",a,b);
}
Output: 1 2
(7) What will be output of following program?


void  convention(int,int);
void main(){
    int a=1;
    clrscr();
    convention(a,++a);
    getch();
}
void  convention(int a,int b){
    printf("%d %d",a,b);
}
Output: 2 2

How to pass parameters in the function in C

Parameter of function can be passed in two ways:
1. Pass by value
2. Pass by address

Feature of function’s parameter in c

1.Default parameter of function is void.
2.Only register storage class is allowed with function parameter.
int devD(register int,register int);
void main(){
    int temp;
    temp=devD(5,25);
    printf("%d",temp);
    getch();
}
int devD(int register x,int register y){
    static int num;
    num=~x+y+1;
    return num;
}
Output: 20
3.Default storage class of function parameter is auto.
4.If function declaration has not written then during the function call it doesn’t check prototype of function if function return type is int data type and parameter is not float data type.
Examples:
(1) What will be output of following program?


void main(){
    call(2);
    getch();
}
int call(long int p,long int q, long int r){
    printf("%d %d %d",p,q,r);
    return 0;
}
Output: 2 garbage, garbage
(2) What will be output of following program?


void call(float,float,float);
void main(){
    call(5.0f);
    getch();
}
void call(float x,float y,float z){
    printf("%f %f %f",x,y,z);
}
Output: Error, too few parameters
(3) What will be output of following program?


void main(){
    call('A');
    getch();
}
int call(char x,char y,char z){
    printf("%c %c %c",x,y,z);
    return 0;
}
Output: A garbage, garbage
(4) What will be output of following program?


void main(){
    call(5);
    getch();
}
int call(int x,int y,int z){
    printf("%d %d %d",x,y,z);
    return 0;
}
Output: 5 garbage, garbage

Google