This blog is under construction

Saturday, 12 May 2012

Library functions and user defined functions in C

C Language supports two types of functions.  They are
          1. Pre-defined functions or Library functions
          2. User defined functions

Difference between library function and user defined functions:
Pre-defined Functions:
Pre-defined functions are built in functions.  Users can only use them, but they cannot modify or understand the internal working logic of those functions(until and unless they have source code access).

     Example: printf(), scanf(), sqrt(), ceil(), floor() etc.

User defined functions:
     Functions that are defined by the users based on their requirements to do a specific tasks are called user defined functions.  User can modify these function according to their needs.


Example C program using library functions and user defined functions
 
  #include <stdio.h>
  int add(int, int);
  /*
   * printf(), scanf() - Library functions
   * add() - user defined function
   */
  int main() {
        int a, b, sum;
        printf("Enter your 1st input:");
        scanf("%d", &a);
        printf("Enter your 2nd input:");
        scanf("%d", &b);
        sum = add(a,b);
        printf("Sum of 2 numbers:%d\n", sum);
        return 0;
  }

  int add(int a, int b){
        int ret;
        ret = a + b;
        return (ret);
  }

  Output:
  jp@jp-VirtualBox:~/cpgms/functions$ ./a.out
  Enter your 1st input:30
  Enter your 2nd input:50
  Sum of 2 numbers:80



No comments:

Post a Comment