I dont know how to call a function! The books just explains but it doesnt show an example. Can someone show me a example or examples?
I dont know how to call a function! The books just explains but it doesnt show an example. Can someone show me a example or examples?
How would that call a function? I just told u what im tryin to learn to do. How do u call a function? Like say u have a problem. U do a program. After u have return 0; then u have another function after it. Then u want those results in your main function. Well u have to call it in your main function. How do u do that?
That is an example of a call to the printf function.Originally Posted by jturner38
If your function is named f and takes no arguments, then you would write:Originally Posted by jturner38
If it returns some value that you wish to assign to a variable x, then you would write:Code:f();
Also, I am assuming that you mean to say that you defined a function after your main function. If so, you should declare a prototype for your function before the point where you define your main function. If you mean to say that you want to call a function after returning 0 from the main function, then that would be wrong, since your function call would be unreachable.Code:x = f();
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Example:
Code:#include <stdio.h> /* Standard I/O header */ int AddNums( int , int ); /* Function Declaration, AddNums is called in main, which is before the AddNums function so function is declared here */ int main(void) { int answer1; /* Variable to hold an answer */ int answer2; /* Same as above */ answer1 = AddNums( 123, 321 ); /* Call to AddNums, will add 123 and 321 (just random numbers I thought of) and put result in answer1*/ answer2 = AddNums( 789, 987 ); /* Another call to AddNums, will add 789 and 987 */ printf("Answer1 = %d\nAnswer2 = %d", answer1, answer2); /* Will print values of answer1 and answer 2 */ return 0; } /* AddNums Function, takes in two numbers, adds them together and returns result*/ int AddNums( int a, int b ) { return a + b; /* Return the sum of a and b */ }
"How do you add two numbers together?"
Asking a series of infinite questions within the same class as that, is no way to learn C. Does your book not explain these things?