Question-Write a function which receives a float and an int from
main( ), finds the product of these two and returns the product
which is printed through main( ).
Solution-
it's a good question, i would like to give you 2 possible solution of this question.
1st solution - Call by Value
#include<stdio.h>
int main()
{
float product(int, float);
int a;
float b,c;
c=product(a,b);
printf("Product=%f\n",c);
return 0;
}
float product(int a, float b)
{
float c;
c=a*b;
return(c);
}
/*Obviously this method requires more effort , sometimes it causes serious error if you don't define function first like i did, so to avoid that i usually prefer call by reference that's a really reliable method, check out the solution by call by reference */
2nd solution - Call by Reference
#include<stdio.h>
int main()
{
int a;
float b,c;
product(&a,&b,&c);
printf("Answer=%f\n",c);
return 0;
}
product(int *a,float *b,float *c)
{
*c=(*a)*(*b);
return (*c);
}
main( ), finds the product of these two and returns the product
which is printed through main( ).
Solution-
it's a good question, i would like to give you 2 possible solution of this question.
1st solution - Call by Value
#include<stdio.h>
int main()
{
float product(int, float);
int a;
float b,c;
c=product(a,b);
printf("Product=%f\n",c);
return 0;
}
float product(int a, float b)
{
float c;
c=a*b;
return(c);
}
/*Obviously this method requires more effort , sometimes it causes serious error if you don't define function first like i did, so to avoid that i usually prefer call by reference that's a really reliable method, check out the solution by call by reference */
2nd solution - Call by Reference
#include<stdio.h>
int main()
{
int a;
float b,c;
product(&a,&b,&c);
printf("Answer=%f\n",c);
return 0;
}
product(int *a,float *b,float *c)
{
*c=(*a)*(*b);
return (*c);
}
No comments:
Post a Comment