User Defined Vs Library Function In C

User- defined Function

A user-defined function, as implied by its name, is a function that a programmer creates to define and execute custom actions within a program. Unlike built-in functions, these user-defined functions are not predefined in C header files, giving programmers the flexibility to modify and tailor them to suit their specific needs. The structure of a user-defined function typically involves three key components: the function declaration, the function definition, and the function call.

Function Definition

The function declaration outlines the precise implementation of the function responsible for executing particular operations within a program.

Syntax:

Example

return_type function_name ( data_type arg1, data_type arg2, ... ) {
// define the variables
statement to be executed;
return (expr);
}

Here, the return type signifies the type of data that a function can return. The return value may include integers, floating-point numbers, characters, doubles, and so on. Meanwhile, the function_name denotes the identifier of a function that comprises multiple arguments within it.

Function Calling

Once the function has been defined, it is essential to invoke the function within the program to carry out its operations. It's worth noting that a function can be invoked repeatedly by specifying the function name along with a list of arguments.

Syntax:

Example

function_name(arg1, arg2, ...)

Here, arg1 and arg2 represent the specific parameters that are provided to the function known as function_name.

Function Declaration

The function declaration specifies the function's name, return type, and its parameters. In C programming, a function definition is always located outside the main function within a program.

Syntax:

Example

return_type function_name (  data_type arg1, data_type arg2, ..) ;

Program to calculate the area of circle using user-defined function in C

Area.c

Example

#include <stdio.h>
int func(int rad);
int main()
{
int r, result;

printf(" Enter the radius of a circle ");
scanf("%d", &r);
result = func(r);
printf (" The area of a circle is: %d", result);
return 0;
}
int func(int rad)
{
int area;
float pi = 3.14;
area = pi * rad * rad;
return area;
}

Output:

Output

Enter the radius of a circle: 5
The area of a circle is: 78

Program to sum two numbers using user defined function in C

Add.c

Example

#include <stdio.h>
int sum_numbers(int x, int y); // function prototype
int main()
{

int sum; // store the result
int num1, num2; // declare variables
printf(" Enter two numbers \n");
scanf("%d %d", &num1, &num2);

sum = sum_numbers(num1, num2); // function call
printf( " The addition of two numbers is %d", sum);
return 0;
}
int sum_numbers(int x, int y) // pass x and y as the parameters
{
int result;
result = x + y; //add two numbers
return result;
}

Output:

Output

Enter two numbers
5
6
The addition of two numbers is 11

Library Function

The C programming language offers a range of library functions that are prewritten to carry out specific tasks. These functions, also known as built-in or predefined functions, are stored in C header files and retain their original functionality. When incorporating these functions into a program, it is essential to include the corresponding header file to access and use them effectively. This approach eliminates the need to manually write code for common tasks, allowing programmers to directly invoke these functions whenever necessary. Examples of such predefined library functions include printf, scanf, and getch.

Program to demonstrate the library function in C

Built.c

Example

#include <stdio.h>
#include <conio.h>
void main()
{
// use printf() function to print the statement 
printf (" Welcome to the Logic Practice ");
printf (" \n It is a library function in the C program. ");
getch(); // It holds the console screen 
}

Output:

Output

Welcome to the Logic Practice
It is a library function in the C program

In the previous code snippet, we utilize the printf and getch predefined functions, which are already implemented in the C compiler with specific functionalities. As a result, there is no need for us to manually write the entire code for incorporating the printf and getch functions within the C program.

Difference between the user-defined and the library function in C

S.No User defined Function Library function
1. A programmer creates a function according to the requirement of a program, which is called a user-defined function. A function whose prototypes are already defined in the C library is called the library function.
2. A user-defined function is required to write the complete code before using the function in a program. We don't require writing a complete code to use the library function in a program.
3. The name of any user-defined function can change easily. We can't change or modify the name of the library function because the functionality of these functions is already defined in the compiler.
4. A user defined function is not compulsory to use in any C program. We need to use the library function in every C program.
5. A user-defined function does not require writing any code inside the header files.For example: swap() function does not require any header file. All the library functions are predefined inside the header files.For example: printf(), and scanf() function are defined in the stdio.h header file. And the strpcy(), strcmp() function are defined in the string.h header file.
6. A user-defined function is part of a program. Library functions are part of the C header file.
7. The programmer or user define the function at the time of writing the code. The developer in the C compiler predefines the library function.
8. Example: multiply(), sum() divide(), etc. are the user defined or user created function in a program. Example: printf(), sqrt(), strcpy(), etc.

Input Required

This code uses input(). Please provide values below: