A brief intro to functions...
your function declaration goes between your C libraries and the body of main. This simply lets the compiler know that it is going to bump into a function at some point later in the program....
The function declaration takes in as many parameters as you decide to pass into it from main...
suppose you want a function that simply subtracts the points taken off of an exam and returns the students grade...
FUNCTION DECLARATION:
Code:
int studentGrade(int perfectTestScore, int pointTakenOff);
reading it from left to right,
the first int is the return type... since in the function above I am trying to figure out what a student grade is, I am going to return an integer to main...
studentGrade is the name of the function... and you can call it anything you want
int perfectTestScore is a perfect grade, it can be 10, 100, 105, etc... this value is assigned in main.
int pointsTakenOff is the points counted off which is also assigned in main...
once you decide to call the function in main, this is how you do it:
FUNCTION CALL:
Code:
studentGrade(perfectScore, pointsOff);
It is important to know that the variable names in the function declaration are completely independent of the function call in main. If you pass in perfectScore as your first parameter in your function call then once it reaches the actual function body, perfectScore is going to be treated as perfectTestScore.
if you wanted to print out the score in MAIN for example, you would treat your function call as an INTEGER since its return type is int.
Code:
printf("Your score is %d.", studentGrade(perfectScore, pointsOff));