| by Arround The Web | No comments

C User-Defined Functions vs Library Functions

The function is the main building block of a program that occupies some operation that could be used throughout the program. In this article, we will learn about the function of C language with its types with some comparative studies.

Function in C Programming

A function in C programming is a chunk of instructions that accomplishes an action. In C, the function is utilized to divide up the code, which implies that advanced code can be divided into smaller, simpler parts, every one of which executes a particular job.

C Programming has two kinds of functions mentioned below:

1: User-Defined Functions in C

The C language also facilitated its developers to make custom functions; these functions are called user-defined functions. In some cases, a developer doesn’t have pre-defined functions so they can reuse the function operations in the C program. To accomplish the task according to the developer’s specifications, the developer must define a suitable function by himself. Some of the user-defined functions can be multiply(), sum(), divide(), and so on.

Pros and Cons of User-Defined Function

Some key advantages and disadvantages of user-defined functions are:

Pros

  • User-defined functions are not limited to adding prototypes in the C program.
  • Developers have the freedom to choose their own function names.
  • Code can be simpler to understand, debug, and maintain.

Cons

  • It takes time to create a function from scratch since the compiler does not have these functions declared.
  • Certain commands like insert and delete cannot be used inside functions.
  • Developing user-defined functions in C requires a significant amount of logical knowledge.

Example of User-Defined Function

#include <stdio.h>

int add(int x, int y);

int main()

{

  int n1,n2,sum;

printf("Enters two numbers to add: ");

scanf("%d %d",&n1,&n2);

  sum = add(n1, n2);

printf("sum = %d",sum);

  return 0;

}

int add(int x, int y)

{

  int result = x+y;

  return result;

}

In the above code, we ask the user to enter two integers and pass them to a user-defined function called “add” which adds them and outputs the result. The main function then assigns the result to a variable called “sum” and prints it to the console.

Output

2: Library Functions in C

Library functions are already included in compiler specifications, which implies they have been declared in the C libraries. These can be utilized for performing routine tasks like computations, changes, and more. Some basic and well-known library functions in C language are printf(), scanf(), getch(), and return(), along with other functions offered in C libraries.

To use library functions, we must have to define the prototypes of these functions at the top of the C program.

Pros and Cons of Library Functions

Key advantages and disadvantages of library functions are:

Pros

  • The C compiler has pre-set these functions, making them readily available in almost every C program, which makes programming easier.
  • Almost in every C program, we can use these library functions.
  • These library functions are portable and save developers time by eliminating the need to write new code.

Cons

  • Developers are limited to using the syntax of these functions and must use the same name as defined in the C language library to add them to their programs.
  • It is not possible to change the name of a library function because its functionality is already determined by the compiler.
  • The inclusion of library functions in a program results in duplication in the executable, which can be inefficient when trying to resolve bugs.

Example of Library Function

#include <stdio.h>

#include <math.h>

int main()

{

  float num, sq_root;

printf("Enter a number: ");

scanf("%f", &num);

sq_root = sqrt(num);

printf("Square root of %.3f = %.3f", num, sq_root);

  return 0;

}

In the above code, the user is prompted to enter a number. And the input number is stored in a float variable called “num”. The square root is calculated using the library sqrt() function from the <math.h> library, and then the result is stored in another float variable called “sq_root”. The program uses the library function printf() and “%.3f” format specifier to print the numbers with three decimal places.

Output

Conclusion

We have seen the user-defined and library functions in C language. User-defined functions can be re-used in a program and library functions can save development time. We have also seen some advantages and disadvantages of both functions. A developer can code easily by using these functions according to their specifications.

Share Button

Source: linuxhint.com

Leave a Reply