UDF (User Defined Function) in C language

Milan Kathiriya
4 min readDec 26, 2023

--

Types of UDF

First of all, let’s understand the Function itself.

Function

A function is a self-contained block of code that performs a specific task.

Functions provide a way to modularize code, making it more readable, maintainable, and reusable.

Types of Function

There are mainly two types of functions available in C language:

1. Built-in Functions: System-defined functions included in C language.

For example, printf(), scanf(), gets(), puts(), etc…

2. User Defined Functions: Functions that have to be created by users themselves.

For example, red(), white(), rahul(), bob(), etc…

Let’s see the syntax of creating a UDF

Syntax of UDF

Types of UDF

There are mainly by four main types to create a UDF in C language:

  1. Take Nothing, Return Nothing (TNRN)
  2. Take Something, Return Nothing (TSRN)
  3. Take Nothing, Return Something (TNRS)
  4. Take Something, Return Something (TSRS)

All of the above types have another synonym respectively:

  1. Without Parameters, No Return Value (synonym name of TNRN)
  2. With Parameters, No Return Value (synonym name of TSRN)
  3. Without Parameters, With Return Value (synonym name of TNRS)
  4. With Parameters, With Return Value (synonym name of TSRS)

Let’s see each type in detail…

TNRN

The full form of TNRN is “Take Nothing, Return Nothing”. Meanwhile, a synonym is “Without Parameters, No Return Value”.

In this type of UDF, since the function does not take any argument/parameter we call it “Take Nothing/Without Parameter”, and since the function does not return any value from its body we call it “Return Nothing/No Return Value”.

Example of the TNRN function
Use of the TNRN function

TSRN

The full form of TSRN is “Take Something, Return Nothing”. Meanwhile, a synonym is “With Parameters, No Return Value”.

In this type of UDF, since the function does take a single/multiple arguments/parameters we call it “Take Something/With Parameter”, and since the function does not return any value from its body we call it “Return Nothing/No Return Value”.

Example of the TSRN function
Use of the TSRN function

TNRS

The full form of TNRS is “Take Nothing, Return Something”. Meanwhile, a synonym is “Without Parameters, With Return Value”.

In this type of UDF, since the function does not take any argument/parameter we call it “Take Nothing/Without Parameter”, and since the function does return some value from its body we call it “Return Something/With Return Value”.

Example of the TNRS function
Use of the TNRS function

TSRS

The full form of TSRS is “Take Something, Return Something”. Meanwhile, a synonym is “With Parameters, With Return Value”.

In this type of UDF, since the function does take single/multiple arguments/parameters we call it “Take Something/With Parameter”, and since the function does return some value from its body we call it “Return Something/With Return Value”.

Example of the TSRS function
Use of the TSRS function

Nested Function

A nested function usually refers to a function that is defined within another function.

However, it’s important to note that true nested functions (functions defined within functions) are not allowed in C language.

Each function in C must be independent and can’t be defined inside another function.

Here in C language, kindly note that we can only call the function within another function.

We cannot define the function within the function in C language.

Use of the Nested Function

In the above program, you can see the example of the Nested Function. The step-by-step function call and returning function output are denoted by a dashed line and step number along with it.

Recursion

Recursion is a programming concept in which a function calls itself directly or indirectly in order to solve a problem.

A function that calls itself is called a recursive function, and the process of repeatedly executing the same set of instructions is known as recursion.

Use of the Recursion
Recursion Process

In the above program, you can see an example of the process of Recursion. The step-by-step function call and returning function output are denoted by a dashed line. Here, a function called fact() is known as a Recursive function since it's called itself.

Thanks for reading this article ❤️

If I got something wrong 🙈, Let me know in the comments. I would like to improve.

Clap 👏 If this article helps you. Add to your 📑 Reading list for future reference.

Connect with me on Linkedin and Github

--

--