Ad Code

Responsive Advertisement

What is Function in C___ How to write them____

 


                      Function's Definition

Functions are the named block of code that performs some action.

The statements are written in function are executed when it is called by name.

Each function has a unique name.

These are used to do similar kinds of tasks without writing them again and again.

The function provides a STRUCTURE PROGRAMMING.


Benefits:

  •                  Easier to code
  •                       Easier to Modify
  •                       Less programming time
  •                       Reusability 
  •                       Easier to maintain and Debug     
  
Types of Functions:

  •                         Built-in Functions
  •                           User-define Functions

                                            How To Write A Function
Function Definition: 
                                 "A set of statements that explains what a function does"
Function Definition can be written at the following places:

  • Before main() function
  • after main() function
  • In a separate file
Syntax:
             Syntax of Function consists of Function Header and Function Body.

1_Return -type Function-name (Parameters or arguments)
                  2_                                               {
   statement 1;
   statement 2;
   statement 3;
          .
          .
          .
    statement N;

}

1_Function Header
2_Function Body

Examples of Function Header:
  1.       int pak(int)
  • int Return type
  • pak() Function-name(Defined by user)
  • (int) Parameter or argument
    2.      float ABC(void)
  • float Return type
  • ABC() Function-name(Defined by user)
  • (void) Parameter or argument

Program 1:


#include<stdio.h>
#include<conio.h>
int cube(int x)   //Function Header
{
   int y=x*x*x;
   return y;
}
main()
{
  int cube(int);  //Prototype of function
  int a;
  printf("Enter a Number for cube");
  scanf("%d",&a);
  int c=cube(a);  //Function call
  printf("cube of number is %d",c);
}


Program 2:

#include<stdio.h>
#include<conio.h>
void ABC()
{
   int a;
   for(a=1;a<=100;a++)
   printf("Programming makes Life Happy\n");
}
main()
{
   ABC();
}
   //This program will print 100 times 'Programming makes Life Happy
Reactions

Post a Comment

1 Comments