User Defined Function in C (Prototype, Call , Definition & Examples)

C programs are highly dependent on functions. Functions are the basic building blocks of C programs.

Every C program is combination of one or more functions. Execution of program written in C starts from unique function known as main() function and it calls other functions directly or indirectly to share the programming task.

Till now we have seen functions like printf(), scanf(), pow(), sqrt(), clrscr() etc. These types of functions are already defined, compiled and stored in different header file of standard C library.

To use these types of functions all we need to do is to include their respective header file to the program.

These types of functions are known as library functions or built-in functions and are the part of C compiler.

In C programming, we can have another type of functions known as User Defined Functions which are written by programmers according to their requirement at the time of writing a program. Sometimes, these functions are also known as Programmer's Defined Function.

Defining and Using User Defined Functions

Basically a user defined function is a sub-program which groups a number of program statements into a single unit for accomplishing specific task and assigns it a name.

To use user defined function, three steps are involved, they are:

  1. Function Prototype or Function Declaration
  2. Function Call
  3. Function Definition

Function Prototype or Function Declaration

Like any variable in a C program it is necessary to declare a function before it’s use. Declaration of function informs the compiler about the existence of function and it will be defined and used later. This declaration of function is also known as function prototype.

In C programming, function can be declared using following syntax.

Function Prototype Syntax


return_type function_name(type1 var1, ... , typeN varN);

Breaking it Down

return_type can be any valid C data types.

function_name can be any valid C identifiers.

type1, type2, ...., typeN can be any valid C data types and var1, var2, ...., varN can be any valid C variables

Function prototype gives following information about user defined functions

  1. List of data that should be passed to user defined function for its operation with their data types. In the syntax, comma-separated list of variables along with their data types ( i.e type1 var1, type2 var2, ..., typeN varN ) within parentheses after function_name is this list. This list is also known as formal arguments or formal parameters.
    (Note: If this list is empty (i.e. function without arguments) then it should be noted that function does not require any data for its operations.)
  2. Type of value that user defined function returns when it is called or used. This is indicated by return_type at the beginning of the function prototype.
    (Note: If void is used in the place of return_type then it indicates that function does not return anything)

Function Prototypes Examples

EXAMPLE 1:


float  my_sum(float x, float y);

Explanation: Here, my_sum() is user defined function and it takes two arguments of type float and when it is called or used it returns a value of float type.

EXAMPLE 2:


void message();

Explanation: Here, message() is user defined function and it does not take any arguments for its operations and it does not return any value when it is called or used.

Function Definition

In C programming, a user defined function must be defined before it is called or used in the program. Function definition consists all the code required for its implementation.

Function Definition General Syntax


return_type function_name(type1 var1, ... ,typeN varN)
{
   statement1;
   statement2;
   .
   .
   .
   satementN;
   
   return statement;
}

Function Definition Examples

Now we write function definition for the all the examples that are used while declaring function prototypes.

EXAMPLE 1:


float my_sum( float x, float y)
{
   float sum;
   sum = x + y;
   return(sum);
}

Explanation: Here, my_sum() is user defined function which takes two arguments of type float. When it is called or used then it receives data on variable x and y. After receiving data, it processes these data according to requirement and finally it returns a value of type float using return keyword. Value is always returned to that portion from where it is called or used.

In this example, values are received in x and y, we add them and store in variable sum and finally we return value of sum.

EXAMPLE 2:


void message()
{
   printf(“Welcome to C Programming!”);
}

Explanation: Here, message() is user defined function which does not take any arguments. When it is called or used then it prints message “Welcome to C Programming!”. After printing message then program control is transferred back to that portion of program from where it is called or used.

Alternatively, we can use return keyword without returning any value to transfer control from called function to calling function like this:


void message()
{
   printf(“Welcome to C Programming!”);
   return;
}

Function Call

After declaring and defining user defined function, it can be used when needed. Generally, user defined function is used by calling or invoking it by passing required data, if necessary, for its functioning. Using function by providing data required for its functioning is known as function call.

Function Call General Syntax


function_name( var1, var2, ...... , varN );

Here, var1, var2, ......, varN contains some value that are being passed to user defined functions. Since these variables contain actual data which are required for the functioning of user defined function. This comma separated list of variables which contains actual value is known as actual arguments.

Function Call Examples

Now we make function call for all the functions that are declared and defined in the examples of function prototype and function definition sections.

EXAMPLE 1:


float num1=12.34, num2=34.67, result;

result = my_sum( num1, num2 ); /* This is function call */

Here user defined function my_sum() is called with two values num1 and num2 which are of type float because function my_sum() requires two values of type float for its functioning which is defined and declared in prototypes and definition sections. After calling my_sum() function, values of num1 (i.e. 12.34) and num2 (i.e. 34.67) will be copied to variable x and y (Confused? From where x and y came? See function definition for my_sum() in examples section of function definition above. These are the formal arguments used in function definition which are used for receiving data). Here variable result of type float is also used because my_sum() is declared and defined in such a way that it finally returns a value of float type. Value returned by my_sum() function is assigned to variable result.

EXAMPLE 2:


message();

Here user defined function message() is called without any arguments because, in prototype and definition section, it is declared and defined in such a way that it does not require any arguments. Similarly, no variable is used to assign value returned by function because prototypes of this function tells that it does not return any value when called (i.e. void is used in return_type).

Now, we combine all these examples that are discussed in function prototype, function definition and function call to make a complete program.

Program 1


/*
Program: Sum of two numbers using user defined function.
Author: Ramesh Bhandari
Date: Jan 22, 2018
*/

#include<stdio.h>


float my_sum(float x, float y); /* Function Prototype */

void main()
{
 float num1=12.34, num2=34.67, result;
 result = my_sum(num1, num2); /* Function Call */
 printf("Sum is : %f.",result);
}

float my_sum(float x, float y) /* Function definition */
{
 float sum;
 sum = x + y;
 return(sum);
}

Output

Sum is : 47.010000.

Program 2


/*
Program: Displaying some message using user defined function
Author: Ramesh Bhandari
Date: Jan 22, 2018
*/

#include<stdio.h>

void message(); /* Function Prototype */

void main()
{
 message(); /* Function Call */
}

void message() /* Function Definition */
{
 printf("Welcome to C Programming!");
}

Output

Welcome to C Programming!