C User-Defined Functions
C User-Defined Functions
C User-defined functions are functions that are declared and defined by the programmer. User-defined functions consist of the following components:
- Function declaration/ Function Prototype
- Function Definition
- Function Header
- Function Body
- Function Invocation / Function Call
Example
Let’s look at a simple user-defined function example C program. The function Sum() computes the sum of two numbers.
/**
**********************************
* Program Description:
* C User-Defined Functions Demo
* Filename : userdefined.c
* C Tutorials - www.TestingDocs.com
*************************************
*/
#include<stdio.h>
/* Function Prototype */
int sum(int,int);
int main()
{
/* Local variables declaration - main*/
int a = 0, b = 0, c = 0;
// Prompt user to enter a, b
printf("Enter the number a = ");
scanf("%d",&a);
printf("Enter the number b = ");
scanf("%d",&b);
c = sum(a,b); // Function Call
printf("The sum of %d , %d is = %d \n",a,b,c);
return 0;
} // end main
/* Function Definition */
int sum(int x, int y) // Function Header
{
/* Local variables declaration -sum*/
int z;
z = x + y;
return z;
} // end sum
User-Defined Function Definition

Sample Output
Enter the number a = 7
Enter the number b = 9
The sum of 7 , 9 is = 16

The flowchart illustration of the flow control between the main() function and the user-defined function sum().

—
C Tutorials
C Tutorials on this website can be found at: