Functions

Function is a block of code which has a name.Each function has a unique name.They are the building blocks of c programs.Functions perform certain tasks repeatedly.Functions provide structured programming approach.

Structure
prototype;
main();
{
  function call;
  body of program
}
definition


Function consists of two parts
  1. Function header
  2. Function body
Function header
First line of function definition is known as function header.
Syntax
Return-type function name (parameter) ;
{
   statement(s);
   }

Function BodyThe statements or set of statements which are executed inside the function is known as function body.Body of function appears after function header.

Function Declaration/PrototypeFunction prototype provides information to compiler about the structure of the function to be used in the program.Function prototype ends with semicolon.
Syntax
Return-type function name (parameter) ; 

Function Call
A statement that activates a function is known as function call.It is used to invite or call a piece of code or statement. In this case control jumps from main program to that piece of code and then returns back to main program.

Example
#include<stdio.h>
#include<conio.h>
void display ();
main ()
{
   clrscr();
   display();
   getch();
   }
void display (void)
{
  printf("HELLO WORLD");
}Advantages of using functions

  • Easier to code
  • Easier to modify
  • Easier to maintain and debug
  • Reusability
  • less programming time

User Define function
The function written by programmer is known as user defined function.

Built in Function
The function which is a part of language is known as built in function or library function.These functions are stored in header files.They make programming easier and faster.

No comments:

Post a Comment