Functions 2025 Example

Functions - Example #

To implement a function there are generally three steps used:

  1. Function Prototype
  2. Function Definition
  3. 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? #

  1. What is the name of the function?
  2. On what line is the function called?
  3. Does this function have parameters passed to it?
  4. Does this function return data?
  5. What line is the function declared?
  6. What action does this function perform?