A c programming language provides several predefined functions to perform specific tasks and allows a programmer to create its user-defined function. The whole c program can be divided into individual and functional units. It means every function in the program acts as an independent unit to perform a specific task or an operation and is also capable of running separately.

In c, we use functions to avoid code duplication. Suppose we want to perform some operation repeatedly; we must add that part of the code as many times in our program, and the program becomes lengthier, and there will be code duplication. If we use functions, we can use a single function call statement anywhere in the program and as many times. By which programs become more superficial and easy to understand the logic.   

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

What Is a User-Defined Function in C?

C allows users or programmers to create their own user-defined functions. In User-defined functions, the user can give any name to the functions except the name of keywords; therefore, it follows three main parts: function declaration, function definition, and function call. 

Syntax of a Function Declaration:

Return_type function_name(list of arguments);

In the function declaration, we have a return type, function name, list of arguments, and semicolon at the end of the statement. A function declaration is also a function prototype that allows the compiler to know the return type, function name, and the number of arguments. 

Example:

int sum (int a, int b);

From the above-given example, int is a return type. By default, every function return type will be int. If a user doesn't want a function to return any value, we can mention void as a return type. 

Following, we have sum, a function name defined by the user, and then arguments a and b of type integer are passed to the function sum.

Now that you have a brief idea of user-defined functions, moving ahead, let’s understand the structure of a user-defined function.  

Structure of a User-Defined Function

A user-defined function has three important components:

  1. Function declaration
  2. Function definition 
  3. Function call

Let us go through them in detail.

Let’s consider the below-given example of an addition program and understand the structure of a function.

#include <stdio.h>

void sum();  //function declaration

void main()

{

sum();   //function call

}

void sum()   //function definition

{

int a=5, b=10,c;

c=a+b;

printf("addition of %d and %d is: %d",a,b,c);

}

Output:

UD_Fun_In_C_1

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Function Declaration

void sum(); 

A function declaration is also known as a function prototype. If the function definition is mentioned before the main function, then the function prototype is not necessary to mention; we can eliminate the function declaration statement. The function declaration statement must always match the function name and the number of arguments passed with the function definition.  

From the given example, function name, sum, and the return type void must match both the statements, function declaration, and function definition. A function declaration statement contains the return type, function name ( list of arguments if required), and semicolon at the end of the statement.

Function Definition

void sum()   //function definition

{

int a=5, b=10,c;

c=a+b;

printf("addition of %d and %d is: %d",a,b,c);

}

The body of function definition consists of a set of statements to perform a specific operation or task. The compiler will return to the function definition whenever a function is called and execute those statements.

According to the given example, the function's body consists of statements to perform the addition operation. Variables a and b hold the values 5 and 10, respectively, and variable c will have the result value of 5+10 =15.

Function Call

sum(); 

Once defining a function definition, there must be a function call in the program. Suppose a compiler encounters a function call anywhere in the program. It will transfer its control to the function definition and execute a set of statements inside the body of a function. A function can be called n number of times in a program.

Considering the given example, sum(); is the function call. Since no arguments are passed in the function declaration and function definition, we don’t provide them in the function call. 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Methods to Call User-Defined Functions in C

  1. Function with no argument and no returning value
  2. Function with argument and no returning value
  3. Function with no argument but has a returning value
  4. Function with argument and returning value

Now, you will go through them in detail.

Function With No Argument and No Returning Value

In this method, we don't pass any arguments and mention void as a function return type, so a function will not return any value. We declare a few variables in the function definition and perform certain operations on them.

Let’s try to understand the method to call a function with no arguments and no returning value.

Example:

#include <stdio.h>

void product(); //function declaration

 void main()

{

  product(); //function call

}

void product() //function definition

{

  int a=5, b=10,c;

  c=a*b;

  printf("product of two numbers=%d",c);

}

Output:

UD_Fun_In_C_2.

The above code shows that the product is a user-defined function with no return value and no arguments passed. The product function will perform the multiplication operation on the variables a and b, holding a value of 5 and 10, respectively. 

Let’s look at another example, the factorial program.

#include<stdio.h>  

void factorial();

void main()    

{    

 factorial();    

}   

void factorial()

{

  int i,fact=1,num;      

  printf("Enter a number: ");    

  scanf("%d",&num);    

    for(i=1;i<=num;i++){    

      fact=fact*i;    

  }    

  printf("Factorial of a number %d is: %d",num,fact);  

}

Similar to the previous program, here also, we haven't passed any arguments to the function, and no return value.

In the factorial function, the function will find the factorial of a given number using the formula fact = fact*i, and the function doesn't return a value. Using printf(), we print the factorial of numbers.

Output:

UD_Fun_In_C_3

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Function With Argument and No Returning Value

In this function call method, with an argument and no returning value, we pass arguments to the function, but the function doesn't return a value.

The example program's arguments passed to the function are a and b of type integer. And it is also necessary to give the arguments in the function call.

#include <stdio.h>

void product(int a, int b);

 void main()

{

int a=5,b=10;

product(a,b);

}

void product(int a, int b)

{

int c;

c=a*b;

printf("product of two numbers=%d",c);

}

Output:

UD_Fun_In_C_4

Following, we have another example, a factorial program of function with argument and no returning value.

Below we have another example program.

Example:

#include<stdio.h>  

void factorial(int fact);

void main()    

    int fact=1;

 factorial(fact);    

}   

void factorial(int fact)

{

  int i,num;      

  printf("Enter a number: ");    

  scanf("%d",&num);    

    for(i=1;i<=num;i++)

    { 

      fact=fact*i;    

    }    

  printf("Factorial of a number %d is: %d",num,fact);  

}

Output:

UD_Fun_In_C_5

The following method is, Function with no argument but has a returning v.alue

Function With No Argument but Has a Returning Value

In this function call method, we don’t pass any arguments in the function but mention a return type. A return type can be any data type; by default, the return type is int, and the value returned by a function is an integer value.

Example Program:

#include <stdio.h>

int product();

void main()

{

printf("product of two numbers=%d",product());

}

int product()

{

int a=5, b=10,c;

c=a*b;

return(c);

}

Output:

UD_Fun_In_C_6.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Example Program:

#include<stdio.h>  

int factorial();

void main()    

    int num;

     printf("Factorial of a number is: %d",factorial());  

}   

int factorial()

{

  int i,fact=1,num;      

  printf("Enter a number:");    

  scanf("%d",&num);    

    for(i=1;i<=num;i++)

    { 

      fact=fact*i;    

    }    

 return(fact);

}

Output:

UD_Fun_In_C_7

Up next, we have a function with arguments and returning value.

Function With Argument and Returning Value

This method passes arguments to the function, which will also return a value.

#include <stdio.h>

int product(int a, int b);

void main()

{

int a=5,b=10;

printf("product of two numbers=%d",product(a,b));

}

int product(int a, int b)

{

int c;

c=a*b;

return(c);

}

In this example, we have passed arguments a and b, and the value returned by a function is the resulting value.

Output:

UD_Fun_In_C_8

Example:

#include<stdio.h>  

int factorial(int fact);

void main()    

    int fact=1;

     printf("Factorial of a number is: %d",factorial(fact));  

}   

int factorial(int fact)

{

  int i,num;      

  printf("Enter a number:");    

  scanf("%d",&num);    

    for(i=1;i<=num;i++)

    { 

      fact=fact*i;    

    }    

 return(fact);

}

Output:

UD_Fun_In_C_9.

With that, you can conclude this tutorial on user-defined functions in c.

Accelerate your career as a skilled MERN Stack Developer by enrolling in a unique Full Stack Developer - MERN Stack Master's program. Get complete development and testing knowledge on the latest technologies by opting for the MERN Stack Developer Course. Contact us TODAY!

Next Steps

"Data Structures in C" can be your next topic. So far, you have learned a User-defined function in C. The following fundamentals will be the data structures and the varieties in data structures used for different purposes.

If you are interested in building a career in software development, then feel free to explore Simplilearn's Courses that will give you the work-ready software development training you need to succeed today. Are you perhaps looking for a more comprehensive training program in the most in-demand software development skills, tools, and languages today? If yes, our Full Stack Developer - MERN Stack should be the right thing for your career. Explore the course and enroll soon.

Please let us know in the comment section below if you have questions regarding the "User-defined Functions In C ” tutorial. Our experts will get back to you at the earliest.

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 24 Feb, 2025

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 8 Jan, 2025

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 8 Jan, 2025

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449