User Defined Function in C4 min read

In this tutorial, we will learn abt the user-defined function in C programming language. Let us start by understanding what is user-defined function in C.

A function refers to a block of code that performs a specific task.

User-defined functions are the ones that are defined by the users according to their needs and they perform the task assigned by the users.

Let us say that you want to calculate the area of a circle and also find the circumference of that circle, you can do so by creating two different functions and pass the radius value to those functions. The two functions could be:

  • circleArea()
  • cricleCircumference()

Example of a simple C user-defined function. The program has a separate function to perform the addition of two numbers.

Output:

Enters the first number: 5
Enters the second number: 8
Result: 13

Considering the above example, let us go through the following topics:

  • function prototype/declaration
  • function definition
  • function call
  • return statement

Function Prototype

Declaration of user-defined function in C.

A function prototype in C is simply a declaration of a function before the main function that does not contain the body of the function. It only specifies the name of the function, its parameter, and the return type of the function.

Syntax of function prototype:

return_type function_name(data_type argument1, data_type argument2, ...);

This line in a program alerts the compiler that the function may be used later in the program.

In the above program, int addFunction(int x, int y); is the function prototype.

Note: Also note that, if you defined the function before the main function then the separate function declaration is not required in a program else you need to mention the declaration. Example: In the syntax below, a declaration is not required


Function Definition

Defining a function in C: It is the expansion of function declaration or prototype. It contains the codes within the curly braces {} that perform some specific task.

How to define functions in c:

Explanation of the above syntax:

  • Return type: A function may return a value unless it is a void function, which does not return any type of value. return_type is a data type of the function. It could return arithmetic values (int, float, etc.), pointers, structures, etc.
  • Function name: Function name refers to identifiers through which function can be identified uniquely. It is a function’s name that is stated to function by following some naming rules.
  • Parameter list: Parameter is also referred to as Argument. We can pass the values through the parameter when the function is invoked. The parameter list refers to the type, order, and number of the parameters of a function
  • Function Body: The function body is enclosed within the curly braces'{}’. It defines the task of a method within the curly braces.

Example:


Function Call

We call the function by its name and pass the argument(if any) where we want to execute that function. The syntax for calling a function:

function_name(argument_list);

In the above program, result = addFunction(num1, num2);is the function call.


Passing arguments to a function

In C programming, most of the time we create a function or declare a function that takes and arguments. We call the function by passing a value or more than one value that is referred to as an argument.

These arguments are the variable that is passed while calling a function.

In the example above, result = addFunction(num1, num2); is the function call and num1 and num2 are the argument that are passed.


Return statement

The return statement in the function is used to terminate the function and return the value to the program line where it is called. The execution control is transferred to the calling function in the program.

Syntax:

return (expression);

In the above program, return sum; is the return statement of the user defined function.

However, a function can also be called without passing an argument.


Go through the C program below to practice user defined functions programs in C and also learn how to pass an argument in a function.


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More