Sunday, 19 September 2010

13. Write yout own functions


  Now I am going to write about how to create a function. If you want to create your own function you can do this. Functions are the building of C. So  for, the programs you have see included only one function: main(). Most real-world programs however, will contain many functions. The general form of a C program that has multiple functions is shown here: 

/*include header files here*/
/*function prototypes here*/
int void main()
{
/*-----*/
}
ret-type f1 (param-list)
{
/*-----*/
}
ret-type f2(param-list)
{
/*------*/
}
.
.
.
.
ret-type fN(param-list)
{
/*------*/
}

You can call your functions by different names. Here, ret-type specifies the type of data return by the function. If a function does not reutrn a value then it's returned type mast be void and if a function does not use parameters, then its param-list should contain the keyword void.
   Now I am going to write another example.

#include<stdio.h>
#include<conio.h>
int funct1(int a, int b);
main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=funct1(a,b);
printf("%d",c);
}
funct1(int a, int b)
{
int c;
c=a+b;
return(c);
}
  
In this program I use a function called funct1. Remember that if we declared one variable under one function that variable can not known in other function. I mean we can not use that variable in another  function.  In that program after the library function I write function prototypes (int funct1 (int a, int b)). Here int is return type of this function and in the bracket it is called parameter. C=funct1(a,b); In this line i called the function. You must try it. OK good luck.
(Post by arnob)

No comments:

Post a Comment