Functions - Example
#
To implement a function there are generally three steps used:
- Function Prototype
- Function Definition
- Function Call
An example of a function and how it is called is shown below.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <stdio.h>
//*********************************************//
int min(int a, int b); //Function Declaration
//*********************************************//
//****************MAIN*************************//
int main() // The MAIN function
{
int a = 6, b = 12, c = 10; //Local Variables to main()
c = min(a,b); //FUNCTION CALL
printf("c = %d", c);
return 0;
}
//***************MAIN END**********************//
//*********************************************//
// Function Definition
int min(int num1, int num2) {
int result; //local variable declaration/
if (num1 > num2)
result = num2;
else
result = num1;
return result; // The value of result is passed back to
// the main() function and stored in the variable, c.
}
//*********************************************//
|
Questions?
#
- What is the name of the function?
- On what line is the function called?
- Does this function have parameters passed to it?
- Does this function return data?
- What line is the function declared?
- What action does this function perform?