Back to C Programming
2026-07-125 min read

Example: User-defined-function

Learn Example: User-defined-function step by step with clear examples and exercises.

Why This Matters

User-defined functions are a fundamental aspect of C programming that allow you to organize your code, reuse functionality, and make your programs more modular and maintainable. Understanding how to create and use user-defined functions is crucial for writing efficient and effective C programs. In this lesson, we will explore the concept of user-defined functions in C, providing practical examples, common mistakes, practice questions, and frequently asked questions.

Prerequisites

Before diving into user-defined functions, it's essential to have a solid understanding of the following topics:

  1. Basic C syntax: variables, data types, operators, and control structures (if...else, for, while)
  2. Arrays in C
  3. Pointers in C
  4. File handling in C

Core Concept

In C programming, a user-defined function is a block of code that performs a specific task. You can create your own functions to suit your needs, making your programs more organized and reusable.

Declaring a Function

To declare a user-defined function in C, you use the return type function_name(parameters) syntax:

return_type function_name(parameter1, parameter2, ...);

The return_type specifies the data type of the value that the function will return. If a function does not return a value, you can use void as the return type. The function_name is the name you give to your function, and the parameters are optional inputs that the function may require.

Here's an example of declaring a simple user-defined function in C:

// Function declaration
void greet(char *name);

// Function implementation (later)

Function Implementation

After declaring the function, you can implement its logic by providing the code that will be executed when the function is called. To do this, you must define the function body within curly braces {}. Here's an example of implementing the greet() function:

// Function implementation
void greet(char *name) {
printf("Hello, %s!\n", name);
}

In this example, the function takes a string (char *name) as an input and prints a greeting message using printf().

Calling a Function

To call a user-defined function in C, you simply use its name followed by parentheses containing any required parameters:

// Call the greet function with "Alice" as an argument
greet("Alice");

When this line of code is executed, the greet() function will be called, and the provided string ("Alice") will be passed as an argument. The function's logic will then be executed, resulting in the output:

Hello, Alice!

Worked Example

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

// Function declaration
unsigned long long factorial(unsigned int n);

// Function implementation
unsigned long long factorial(unsigned int n) {
unsigned long long result = 1;

for (unsigned int i = 2; i <= n; ++i) {
result *= i;
}

return result;
}

// Main function
int main() {
unsigned int number = 5;
unsigned long long factorial_result = factorial(number);

printf("Factorial of %u is: %llu\n", number, factorial_result);

return 0;
}

In this example, the factorial() function calculates the factorial of a given number by multiplying all integers from 1 to the input number. The main function calls the factorial() function with an input of 5 and stores the result in factorial_result. Finally, it prints the calculated factorial value.

Common Mistakes

  1. Not returning a value: If a function is supposed to return a value but does not have a return statement, it will typically cause issues when trying to use the returned value elsewhere in your program.
  1. Incorrect parameter types or count: Using incorrect data types for function parameters or providing the wrong number of arguments can lead to runtime errors and unexpected behavior.
  1. Not defining a return type: If a function is supposed to return a specific data type but does not have that return type declared, it may cause issues when trying to use the returned value elsewhere in your program.
  1. Function redefinition: In C, functions with the same name and parameter list must have identical implementations. Redefining a function can lead to unexpected behavior or compile-time errors.
  1. Not initializing variables: Variables used within a function should be initialized before being used to avoid undefined behavior.

Practice Questions

  1. Write a user-defined function in C that calculates the sum of two integers and returns the result.
  2. Create a function in C that checks whether a given number is prime or not.
  3. Implement a function in C that sorts an array of integers using bubble sort.
  4. Write a user-defined function that finds the maximum value in an array of integers.
  5. Create a function in C that reads a line from a file and returns the length of the line as an unsigned integer.

FAQ

  1. What happens if I don't return anything from my function? If a function does not have a return statement or is declared with the void return type, it will not return any value when called. However, if a non-void function does not return a value before reaching its end, it may cause unexpected behavior in your program.
  1. Can I pass an array as a parameter to a user-defined function? Yes, you can pass arrays as parameters to user-defined functions in C. To do this, you must use pointers to the first element of the array.
  1. How can I make my function recursive? To make a function recursive, you need to call the function itself within its own implementation. The base case should stop the recursion when the problem has been solved or reduced to a smaller, solvable instance.
  1. What is the difference between static and extern functions in C? Static functions are only visible within their own file (compilation unit), while extern functions can be defined in multiple files and have global visibility. Extern functions are typically used for function prototypes, while static functions help with code organization and encapsulation.
  1. Can I overload functions in C? No, C does not support function overloading like some other programming languages. However, you can achieve similar functionality by using different parameter lists or data types for your functions.