Back to C Programming
2026-07-126 min read

User-defined Function in C programming

Learn User-defined Function in C programming step by step with clear examples and exercises.

Why This Matters

Welcome to this full guide on user-defined functions in C programming! In this tutorial, we'll look closely at the world of custom functions that allow you to perform specific tasks according to your needs. By the end of this lesson, you'll have a thorough understanding of how to create and use user-defined functions in C, along with common mistakes to avoid and practice questions to test your skills.

Why This Matters

User-defined functions are essential for structuring your code effectively and reusing blocks of code. They help make your programs more modular, readable, and maintainable. In real-world scenarios, user-defined functions play a crucial role in solving complex problems, debugging, and optimizing code efficiency.

Prerequisites

To follow this tutorial, you should be familiar with the following C concepts:

  1. Basic syntax (variables, constants, operators)
  2. Control structures (if-else statements, loops)
  3. Data types (int, float, char, etc.)
  4. Arrays and pointers
  5. Understanding of call stack and recursion (for understanding recursive functions)

Core Concept

A user-defined function is a block of code that performs a specific task. C allows you to create custom functions according to your needs. Let's break down the components of a user-defined function:

return_type function_name(parameters) {
// Function body
}
  1. return_type: The data type of the value that the function will return (optional). If no return type is specified, the default is void.
  2. function_name: A unique identifier for your custom function.
  3. parameters: Zero or more variables that are passed to the function when it's called.
  4. Function body: The block of code that contains the instructions for the function to execute.

Defining a User-defined Function

To define a user-defined function, follow these steps:

  1. Declare the function prototype outside the main function:
return_type function_name(parameters);
  1. Define the function body inside the main function or another function:
void greet(char *name) {
printf("Hello, %s!\n", name);
}

Calling a User-defined Function

To call a user-defined function, use its name followed by the required parameters in parentheses:

int main() {
char name[] = "John";
greet(name); // Calls the greet function with the parameter 'name'
}

Worked Example

Let's create a user-defined function that calculates the factorial of a number using recursion:

#include <stdio.h>

int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}

int main() {
int num = 5;
printf("Factorial of %d is: %d\n", num, factorial(num));
return 0;
}

In this example, we define a recursive function called factorial. The base cases are when n equals 0 or 1, in which case the function returns 1. Otherwise, it multiplies n by the result of calling itself with n - 1. In the main function, we call the factorial function with a specified number and print the result.

Worked Example

Let's create another user-defined function that checks if a number is prime or not:

#include <stdio.h>

int is_prime(int num) {
if (num <= 1) {
return 0; // Not prime
}

for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
return 0; // Not prime
}
}

return 1; // Prime
}

int main() {
int num = 17;
if (is_prime(num)) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0;
}

In this example, we define a function called is_prime. The function checks if the number has any factors other than 1 and itself. If it does, the function returns 0 (not prime). Otherwise, it returns 1 (prime). In the main function, we call the is_prime function with a specified number and print the result.

Common Mistakes

  1. Forgetting to declare the function prototype before using it in the main function.
  2. Not initializing or providing valid parameters when calling a user-defined function.
  3. Returning a value of an incorrect data type from a function with a specified return type.
  4. Not handling the base case correctly in recursive functions (e.g., forgetting to check for 0 or 1 in factorial function).
  5. Forgetting to close the } for the function body.
  6. Using local variables with the same name as global variables inside a user-defined function, causing unintended overwriting of values.
  7. Not properly handling memory allocation and deallocation when using dynamic memory (e.g., malloc).
  8. Forgetting to include necessary header files for certain functions (e.g., stdio.h for printf).

Practice Questions

  1. Write a user-defined function that finds the maximum of two numbers.
  2. Create a function that checks if a number is prime or not using a loop instead of recursion.
  3. Implement a user-defined function that calculates the sum of all digits in an integer.
  4. Write a recursive function to find the Fibonacci series up to a given number.
  5. Define a function that swaps two variables without using a temporary variable.
  6. Write a function that finds the smallest common multiple of two numbers.
  7. Implement a user-defined function that calculates the greatest common divisor (GCD) of two numbers using Euclid's algorithm.
  8. Create a function that sorts an array of integers in ascending order using bubble sort algorithm.
  9. Write a function that finds the power of a number raised to another number without using loops or recursion.
  10. Implement a user-defined function that checks if a given year is a leap year or not.

FAQ

Q: What happens if I don't specify a return type for my user-defined function?

A: If you don't specify a return type, the default is int. However, it's good practice to explicitly declare the return type to indicate the expected result of the function.

Q: Can I call a user-defined function inside another user-defined function?

A: Yes, you can call one user-defined function from within another user-defined function.

Q: What is the purpose of a prototype in C programming?

A: The prototype declares the name, return type, and parameters of a user-defined function before it's defined. This allows the compiler to check for correct usage and potential errors.

Q: How can I pass an array as a parameter to a user-defined function in C?

A: To pass an array as a parameter, you can use pointers. Declare the parameter as a pointer to the array type (e.g., int *arr).

Q: What is the difference between a local variable and a global variable inside a user-defined function?

A: Local variables are declared within the scope of the function and only exist during the execution of that function. Global variables, on the other hand, have global scope and can be accessed from any part of the program.

Q: How can I pass more than one argument to a user-defined function in C?

A: You can pass multiple arguments by separating them with commas in the function call. Inside the function, you can access each argument using its respective name.

Q: What is function overloading in C?

A: Function overloading does not exist in C. Each function must have a unique signature (name + parameters). However, you can achieve similar functionality by creating multiple functions with different numbers or types of parameters and calling the appropriate one based on the arguments passed.