Thursday, July 9, 2009

In C, Do I really have to have a function prototype?? If I do have one, why do I define my function after main

If I do have a prototype, Do I have to define my function after main?? What if I do not??





I am really annoyed by these retarded prototypes! What happenes if I do not put them, then How do I define function and when??





And Finally, why is this an error





#include %26lt;stdio.h%26gt;





int mult (int x, int y);





int main()


{


printf("Hello\n");


mult();





int mult (int x, int y)


{


scanf("%d", %26amp;x);


scanf("%d", %26amp;y);


printf("The result is %d", x * y);


}


It gives me too few arguments for mult!





Pls help me! I am annoyed to maximum by these functions!

In C, Do I really have to have a function prototype?? If I do have one, why do I define my function after main
To use the function "mult" in main, you either have to have a prototype before main or the entire function definition before main. Otherwise, when the compiler gets to the point in main where you call your function (mult) it will complain that it's never heard of anything called "mult" before.





So, you can just put the whole function definition before main, or you can use a prototype, which tells the compiler that a function called "mult" exists, and will be defined later.





As for the compiler error, you declare mult to take two integers, but when you call it you provide none. You're also not returning anything from mult when, as it's defined, it should return an integer. Try changing mult to look like this:





void mult()


{


int x, y;


scanf("%d", %26amp;x);


scanf("%d", %26amp;y);


printf("The result is %d", x * y);


}





This declares the variables x and y inside of mult, rather than specifying them as values that have to be supplied when the function is called. Or you could do this:





#include %26lt;stdio.h%26gt;





int mult (int x, int y);





int main()


{


printf("Hello\n");


int firstNumber, secondNumber, result;


scanf("%d", %26amp;firstNumber);


scanf("%d", %26amp;secondNumber);


resutl = mult(firstNumber, secondNumber);


printf("The result is %d", x * y);


}





int mult (int x, int y)


{


return x * y;


}
Reply:If you define your function before main, then you don't need a prototype. If you design it after main, then you need the prototype to let whatever is in main know that these functions exist.





You called the function mult() with no arguments. You defined the function with the requirement that two integers must be passed as arguments.





Don't get frustrated. Once you get the gist of it things, functions will be in your veins.





main is... well... the main part of your program. Its the first thing that's executed when you run a program. Functions provide a way to do things without knowing how its done. For example if you have an "add" function that takes two integers then return their sum, all you want to know is that it does what it does. How it does it is of no concern... unless you have to write the code for it... or care about performance. That's the black box approach.





The good thing is that functions/methods are basically the same across all languages that you'll most likely use. Understand the concept for one and you'll understand it for all.





Check out this C++ tutorial on functions. The same applies to C.





http://www.cplusplus.com/doc/tutorial/fu...
Reply:2 Errors with your code, one was already mentioned, the other is below:





#include %26lt;stdio.h%26gt;





int mult (int x, int y);





int main()


{


printf("Hello\n");


mult();





int mult (int x, int y)


{


scanf("%d", %26amp;x);


scanf("%d", %26amp;y);


printf("The result is %d", x * y);


}





is missing a "}" before int mult(int x, int y)





You never got out of your main program, and you want to do your scanfs in main


int x,y;


scanf("%d", %26amp;x);


scanf("%d",%26amp;y);


mult(x,y);





Further, your function is set to return a value of int type, but you're not returning anything.





so you could in your mult function you could do:


return x*y;


and do the print in main. Or set the return type to void on your mult function
Reply:1. Prototyping in most C compilers is not required. It DOES help you avoid syntax errors, as the compiler 'checks' all instances of the function for correct values.





2. A couple of problems with your code. You need a closing brace for the Main function. Assuming that's a typo, the next problem is in your definition, both prototype and actual, you're indicating that the mult function will be passed two integers, and you're never passing them. Change the definition to read int mult() in both instances. Then in the mult function, add a line after the first brace "int x,y" to define the integers as a local variable. Alternative solution: define mult as "int mult()" and declare int x,y as a global variable. 2nd alternative: declare x and y in the main function, perform the scanf functions in Main, then pass the values to mult


No comments:

Post a Comment