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

C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a number using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …