C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. Functions
  5. Types of Function

Types of Function

C has two main types of functions:

  • These are predefined functions provided by the C standard library.
  • You can directly use them by including appropriate header files.
  • Examples: printf(), scanf(), strlen(), sqrt(), strcpy(), etc.

Example of a Library Function:

#include <stdio.h>
#include <math.h>

int main() {
    double result = sqrt(25.0);  // sqrt is a library function
    printf("Square root: %.2f", result);
    return 0;
}
  • These are custom functions written by the programmer.
  • You define them to perform specific tasks that are not available as library functions.
  • These functions enhance the modularity of the program.

Example of a User-Defined Function:

#include <stdio.h>

// Function declaration
int add(int a, int b);

// Main function
int main() {
    int result = add(5, 7);  // Function call
    printf("Sum = %d", result);
    return 0;
}

// Function definition
int add(int a, int b) {
    return a + b;
}
Difference Between Library Functions and User-Defined Functions

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *