Thursday, July 18, 2013

Functions in C

Functions are nothing but a piece of code which is meant to do some task out of the main program, which can be reusable. In C, we write functions to make program simple. Suppose we want to find the square of a number ten times in a program, instead of writing ten times the same code with different numbers, we can write a function which takes a number and gives the square and use the function every time we need the function.

Functions are basically of three types.



  1. Functions with no return type and no parameters
  2. Functions with no return type and having parameters
  3. Functions with return type and parameters.
A function is more precisely defined as a named, independent piece of code.
  • A function is named- it takes a unique name and this name should not be used for any other function, not even for variables and the name should follow the rules of variable names.
  • A function is a independent piece of code i.e, it should not be developed in any function. It should be developed individually and can be used anywhere.
  • A function is reusable i.e., a function written once can be used anywhere in the program and we can use any number of times.
  • A function is meant to perform specific task i.e., a function is generally written to make it perform some task.
Example:
  • #include<stdio.h>
  • main()
  • {
  • display();
  • }
  •  
  • void display()
  • {
  • printf("In function");
  • }
In the later chapters, we will know about the functions more.