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

Find a pair with the Given Sum in an Array in Java

In this tutorial, we will write a java program to find the sum pair in an array. Before that, you may go through the following …
Read More

C++ Program to display Pascal Triangle

In this tutorial, we will write a C++ program to display pascal’s triangle. Before that, you may go through the following topic in C++. for …
Read More

C++ Program to Calculate Sum of Geometric Progression

In this tutorial, we will learn and write a program to find the sum of GP series in C++. Before that, you may go through …
Read More

C++ Program to find the Transpose of a Matrix

In this tutorial, we will learn and write a program to find the transpose of matric in C++. Before that, you may go through the …
Read More

How to Split a String in C++

Let us learn how to split string in C++ programming language. We will look at a few ways to split a string. The splitting of …
Read More

C++ Program to calculate Average of Numbers using Arrays

In this tutorial, we will learn and write a C++ program to find the average of numbers using an array. To understand the coding, you …
Read More