C Programming/C Function

User-Defined Functions

Updated on January 9, 2026
2 min read

What is a User-Defined Function?

user-defined function is a function created by the programmer to perform a specific task.

Definition:

User-defined functions are functions written by the programmer to perform specific operations.

Why User-Defined Functions Are Needed

  • Break large programs into small parts
  • Reuse code multiple times
  • Improve readability
  • Easy debugging

Example: User-Defined Function

c
1#include <stdio.h>
2
3// User-defined function
4void greet() {
5    printf("Welcome to C Programming");
6}
7
8int main() {
9    greet();   // Function call
10    return 0;
11}

How User-Defined Function Works

  1. Function is defined by programmer
  2. Function is called from main()
  3. Control transfers to function
  4. Function executes
  5. Control returns to main()

3. Key Differences (Exam Table)

FeatureLibrary FunctionUser-Defined Function
Written byC libraryProgrammer
Code visibleNoYes
ModificationNot allowedAllowed
Header fileRequiredNot required
Examplesprintf()scanf()sum()greet()

4. Simple Summary for Students

  • Library functions are ready-made tools
  • User-defined functions are custom tools you create
  • Both help make programs shorter and cleaner

Part of User Defined Functions

user-defined function in C has three main parts

Understanding these parts is mandatory for exams and basics.

1. Function Declaration (Prototype)

What is it?

It tells the compiler:

  • Function name
  • Return type
  • Parameters (if any)

It is written before main().

Syntax

return_type function_name(parameters);

Example

int add(int, int);

Meaning:

  • Function name: add
  • Return type: int
  • Takes two integers

Why is it needed?

  • Compiler knows the function exists
  • Prevents calling errors

2. Function Definition

What is it?

It contains the actual code (logic) of the function.

It can be written:

  • After main() (most common)
  • Or before main()

Syntax

return_type function_name(parameters) {
    // function body
}

Example

int add(int a, int b) {
    return a + b;
}

Here:

  • a and b are parameters
  • return sends value back

3. Function Call

What is it?

It is the place where the function is executed.

Usually written inside main().

Syntax

function_name(arguments);

Example

sum = add(5, 3);

Complete Program

c
1#include <stdio.h>
2
3// 1. Function Declaration
4int add(int, int);
5
6int main() {
7    int result;
8
9    // 3. Function Call
10    result = add(5, 3);
11
12    printf("Sum = %d", result);
13    return 0;
14}
15
16// 2. Function Definition
17int add(int a, int b) {
18    return a + b;
19}

How Control Flows

  1. Program starts from main()
  2. Function call is found
  3. Control goes to function definition
  4. Function executes
  5. Value is returned
  6. Control comes back to main()
Note: A function in C consists of declaration, definition, and function call.
User-Defined Functions | C Programming | Learn Syntax