Ok so to make a simple function you need three things..
1) you need to declare it before main or in a seperate header file much like you did with the struct.
2) you need to make the funtion, it can be written after main or put in a seperate header file.
3) call the function in main and pass in any variables that you are using.
When I first started I found it made the most senst for me to build my function first and I can think of what parameters to use and such. Lets make a function tha adds to integers and returns the result. So what is going to happen is that your going to provide the function with numbers to use when you call it in main. Your going to do something with these numbers, in this case add them, and then return the result back to main so you can use it. C
First we tell the program what kind of data we are returning back to main by typing the type out first (i.e. int, double, char ect...) ths case its an int.
Next we name the function, lets call this one GetSum
Then we tell the program what input we are expecting to pass in by providing the type and a name for each variable. Then we can write our function and return a value. Heres what this would look like...
int GetSum ( int firstValue, int secondValue )
{
int sum = 0; // initialize the sum value for when we add the two numbers being passed in
sum = firstValue + secondValue; // add two numbers
return sum; // this returns the sum we just got so you can use it in main
} // end function
You would put this part after main
If you dont declare this function before main, your program will not know what you are referring to when you use it. It would be like you trying to access your struct without declaring it before your main function.
Your declaration looks excatly like the first line of your function except that you add a semicolon at the end like a statement... It looks like this...
int GetSum ( int firstValue, int secondValue ); // declaration before main
now your funtion is ready to use. All you need to do is call it in main. Remember that it returns a value and we also need to pass in two numbers for it to work. If you look at the declaration what is first? Its the int, its what the function returns, then the name, then the values you pass in, in this order. Think about this when you use it in main.
lets say you have two values x and y that you want to add. And you want to get the sum of these from your function. This is how you'd use it in main...
sum = GetSum( x, y );
see how the order goes in main. Compared to how its declared?
sum = GetSum ( x, y );
int GetSum ( int firstValue, int secondValue)
Now you have all you need to write a basic function. You can pass in as many variables as you want and any type you want. But you can only return one thing for each function. There are ways around this but for now just know you can only return one thing at a time.
I'll code how the overall structure so you can get a feel for how it looks in a program...
#include "stdio.h"
struct num
{
int x;
int y;
int ans;
};
int GetSum ( int firstValue, int secondValue );
int main ( void )
{
struct num P;
P.x = 1;
P.y = 5;
P.ans = GetSum ( P.x, P.y ); // use funtion to get sum
printf ("The Result is: %d",P.ans); // print sum out
return 0;
}// end main
int GetSum ( int firstValue, int secondValue )
{
return ( firstValue + secondValue );
// returns the result of adding these two numbers.
// NOTE: you can use the way I did this before or this way it doesnt matter
// this way there is just less to write and it looks a little cleaner
}
Hope this helps. And that you really understand what a function is doing. Good Luck!
kustom