# include<stdio.h>
# include<conio.h>
void main()
{
 float square();
float a,b;

printf("\nenter any number");
scanf("%f",&a);

b=square(a);
printf("\nSquare of %f is %f",a,b);

}


float square ( float x)
{
 float y ;
 
y=x*x;
return(y);
}

when i run this i get an error ::----
"" type mismatch in redclaration of 'square"" .

this is an texxt boook example .
about function declaration and prototypes
passing float and char values thru functions
so whtz the glitch can ne1 tell me hopw to overcome
this problem ?????????????????????????????????

I see void main() and a lack of code tags.

No doubt both have been mentioned before, obviously with no impression on you.

Look, the c compilers are made to be fast, there are compilers which use only one pass for finding all the symbols. Now you call your "square" function before you define or declare your function, so the compiler knows nothing about your function when it processes your function call. So you either have to define your function before, or provide a prototype. The latter is especially useful, because you later may want to make your function available to other modules, by include file. As this is so necessary, to declare what functions you would like to make available to the rest of the program, and what not, this is because any more complicated search for function names is not necessary in c. Because it is a good habbit to write function prototypes, and makes the code much clearer.

And, don't make Salem angry with void main, there is a purpose for main to be int, and this has something to do with the rest of your system. When some "simpler" languages maybe are not made to work together with other programs, then c has been used all the time to write all the components of the system, and there, the program is not a stand-alone unit, but has to work together with other programs. The program would not always run separately, but can be used in a shell script as a part of some more complicated processing, or executed from inside of some other program.

# include<stdio.h>
# include<conio.h>
void main()
{
float square(float);//you missed float here as argument
float a,b;

printf("\nenter any number");
scanf("%f",&a);

b=square(a);
printf("\nSquare of %f is %f",a,b);

}


float square ( float x)
{
float y ;

y=x*x;
return(y);
}

hey
thank you alll . i didnt got wht salem meant
mebbe , i m yet too low to understand all tht stuff ... thx letme to tell where i erred .

hmmmm u rite tk tk.....
will try to do so in future

though i didnt understood all the comments on void main() .... made by u and salem
( i asssume u mean i declare main as int ...
not void in future ??? .....
and are void and int only 2 declarations for main ? or can define as float and will it work ? )
default is int i think

>>are void and int only 2 declarations for main

void is never ever a valid declaration for main(). ALWAYS declare it as returning int.

add this between lines 2 and 3 of your program float square ( float x); It will tell your compiler about that function.

I Think There Is Some Inbuilt Function Which Has Same Name As Yuor Square Function. So Try The Same Program By Changing The Name Of Square Function

I Think There Is Some Inbuilt Function Which Has Same Name As Yuor Square Function. So Try The Same Program By Changing The Name Of Square Function

Please don't capitalize every word in the sentence. It makes your post look childish and ridiculous.

I tried by puting your code into c file and compile. It gave me error. there is a function with name square. So in addition to "providing a prototype at start of your program", you also need to change the function name. I would suggest you use "Square" instead of "square".

i thought the functions part would be simple but giving me a lot of headache
now i tried passing 2 values int and float
but again same error ?
can ne1 tell why ?
referring let us c using tc .

assume in square function above what are the lines i need to add for it to work ?
( finding product of 2 values 1 is int and 1 is float ) ?
the prototype helps when ther is singe value
but passes the error too many declarations if used more then 1 value ?

i thought the functions part would be simple but giving me a lot of headache
now i tried passing 2 values int and float
but again same error ?
can ne1 tell why ?
referring let us c using tc .

assume in square function above what are the lines i need to add for it to work ?
( finding product of 2 values 1 is int and 1 is float ) ?
the prototype helps when ther is singe value
but passes the error too many declarations if used more then 1 value ?

Assuming you want a float as a return product:

# include<stdio.h>

float product ( float x, int i);
int main( void )
{
   float a = 2.2;
   int b = 3;

   printf("The product of %.2f and %i = %.2f",a,b, product( a, b ));
   
   getchar();
   return 0;
}

float product ( float x, int i)
{
   return i * x;
}

/* my output:
   The product of 2.20 and 3 = 6.60
*/

Make notice of the change from void main() to int main( void ) which is always the way it should be.

post the code snippet you are refering to.. I will try to answer.

hey aia ...
i almost coded the same stuff as u
the difference was i defined product outside
printf statement ( but tht shldnt be a problem )
cont know why i was getting error
will compile the following code and c if it works

trivedi okay will try to post the code form next time onwards

aaahhhh dont red the above message
i typed it to early ....

aia .... there were sum data types misplacemet
by me was my error
i had created the prototype in main ...
was tht the error ?
shall i create the prototype before main
and for call by references can i do the same thing ....

and thx for all the efforts to solve my queries

1 things for sure u guys are geniuses
how long it took to be perfect ??

>shall i create the prototype before main
Yes. In fact prototypes are usually put inside a header file that then you include like you do with <stdio.h>

>and for call by references can i do the same thing ....
That would be done like this:

# include<stdio.h>

void product ( float x, int i, float *result );

int main( void )
{
   float a = 2.2, product_result;
   int b = 3;
   
   product( a, b, &product_result );
   printf("The product of %.2f and %i = %.2f" , a, b, product_result );
   
   getchar();
   return 0;
}

void product ( float x, int i, float *result)
{
   *result = x * i;
}

hmmm yep , thx for the help solved all my functions query it seems . shldnt have problem in future abt functiosn nemore .

u said " void main() to int main( void ) which is always the way it should be . " -- why is this so ? just dont wanna follow blindly want sum knowledge behind it hope u dont mind

and abt recursions they seem simple
but i have my doubts feel coding simple this way then using recursion --- is it okay if i dont pay much attention on recursion and leave it
just wanted to know ur guys point of view .

>u said " void main() to int main( void ) which is always the way it should be . " -- why is this so ? just dont wanna >follow blindly want sum knowledge behind it hope u dont mind

The C Standard decrees that main returns an integer. It can never be void, since that would not return anything, right?. If you don't use any arguments to it will always be.

int main( void )
{
    return;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.