Types of Function in C ! Library Function in C ! User Defined Function In C ! Function Definition

Categories: C Programming language

  • Library Function
  • User Defined Function

Library Function in C

C provides library functions for performing some operations. These functions are present in the c library and they  are predefined for example. Sqrt() is a mathematical library function  which is used for finding the square root of any number The function scanf and printf() are input and output library function  similarly we have strcmp() and strlen() for string manipulations.

To use a library function we have to include some header file using the preprocessor directive #include . for example : to use input and output function like printf() and scanf() we have to include stdio.h, for math library function we have to include math.h for string library string.h should be included .

User Defined Function In C

A user can create their own functions for performing any specific task of program. These types are called user defined function. to create and use these function we have to know these 3 things.

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

Function Definition

The function definition consists of the whole description and code of a function. It tells that what the function is doing and what are the input output for that. A function is called by simply writing the name of the function followed by the argument list inside the parenthesis.

A function definition have two parts :

  • Function Header
  • Function Body

Structure of a Function

There are two main parts of the function. The function header and the function body.

int sum(int x, int y)
{
int ans = 0//holds the answer that will be returned
ans = x + y; //calculate the sum
return ans //return the answer
}

Function Header

The first line of the above code is called Function Header.

int int x, int y)

It has three parts

  1. The name of the function i.e. sum
  2. The parameters of the function enclosed in parenthesis
  3. Return value type i.e. int

Function Body

What ever is written with in { } in the above example is the body of the function.

Some properties of Functions

  • Every function has a unique name. This name is used to call function from main() function.
  • A function performs a specific task.
  • A function returns a value to the calling program.

Advantages of using Functions in C

  • Functions has top down programming model. In this style of programming, the high level logic of the overall problem is solved first while the details of each lower level functions is solved later.
  • A C programmer can use function written by others
  • Debugging is easier in function
  • It is easier to understand the logic involved in the program
  • Testing is easier

Why we use Function

  1. Writing function avoids  rewriting the same code  over and over. Suppose we have a section of code that is calculating the area of triangle, if later we want to calculate the area of other triangle then we don't need to write the code again.
  2. By using function it becomes easier to write programs in c language.

Top Blogs
Introduction to Implementation of Queue using Linked List Published at:- Input and Output Functions in C Published at:- Importance of C Programming Language Published at:- How does C Programming Language Work Published at:- History of C Language : Introduction to C Programming Language Published at:- Benefits of C language over other programming languages Published at:- C Language Interview Question and Answers Published at:- Pointer declaration in C ,Address operator Published at:- C Program example: Input a number and print sum of its digits Published at:- What values are printed when we run following? Published at:- C Interview Questions And Answers Published at:- Learn C Programming language with example Published at:- C Memory Management ! Dynamic memory allocation Published at:- Structure in C: Introduction Published at:- Loops in C Published at:- Example of Function with no return type and no argument Published at:- Functions with arguments and no return values. Published at:- Functions with arguments and return values -C Examples Published at:- Functions that return multiple values -C Example Published at:- Types of Function in C ! Library Function in C ! User Defined Function In C ! Function Definition Published at:- C Functions ! What is a Function Published at:-
R4Rin Team
The content on R4Rin.com website is created by expert teams.