Functions

Functions in c-:
functions are one of the most important facilities provided by a programming language .
there are several advantages of functions but some of these are listed below .
-:functions shorten the length of large program.
-:functions introduce reusable code to the program.
-:functions bring modularity to our programs and h ence debugging becomes more easy.
-:functions by using different argum ents can produ ce diffrent values.


a sample program with functions -:

#include< stdio.h>
#include< conio.h>
void function()
{
printf("abc\n");
printf("def\n");
}
void main()
{
function1();
printf("**********\n");
function1();
getch();
}
Although functions in c are of two types these are predefined functions and user defined functions ( udf) predefined functions are already defined in different header files hence we do not need to define th em we only call them but user defined functions ar e always to be defined by the user before their ca lling.

program to add two number using functions-:


#include< stdio.h>
#include< conio.h>
void abc()
{
int a,b,c;
printf("enter two numbers");
scanf("%d%d",&a,&b);
c=a+b;
printf("the sum of number is %d",c);
}
void main()
{
clrscr();
getch();
}



calling versus called functions-:
in functional programming those functions that ar e capable to call some other functions are called calling functions .
in contrast those functions that are called by cal ling function are known as called functions.
example-:in the previous program(ie.addition of two number via functions) the main() function is wor king as a caling function while the abc () is work ing like a called function.

function definition versus function calling-:

every function that is being called must be define d somewhere in the program definition means to bun dle the logic and provide a proper name while cal ling means to be called by any calling functions .


function definition-:
void function1()
{
printf("hello");
}
function calling-:
void main()
{
clrscr();
function1();
getch();
}

note point-:In a program although we can define a function anywhere but it is better to define its c alling when we define our functions after calling we must use a prototype for that function.
table using functions -:


#include< stdio.h>
#include< conio.h>
void function1()
{
int i,num,num1;
printf("enter the number");
scanf("%d",&num);
for(i=1;i<=10;i++)
{
num1=num*i;
printf("%d",num1);
}
}
void main()
{
clrscr();
function1();
getch();
}



to find prime or not using functions-:


#include< stdio.h>
#include< conio.h>
void function1()
{
int i,num,c=0;
printf("enter a number");
scanf("%d",&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
{
c=c+1;
}
}
if(c==2)
{
printf("prime");
}
else
{
printf("not prime");
}
}
void main()
{
clrscr();
function1();
getch();
}



fibonnaci series using functions-:


#include< stdio.h>
#include< conio.h>
void function1()
{
int i,j=1,k=0,l=0;
for(i=1;i<=10;i++)
{
l=j+k;
printf("\n%d",l);
k=j;
j=l;
}
}
void main()
{
clrscr();
function1();
getch();
}