Monday, 23 February 2015

FUNCTION IN C



Function
=========
Threre are three steps are necessary to define a function in program.

Steps
=====
1) Function Declaration
2) Function Calling
3) Function Defenition


Function Declaration
====================
Syntax:
======
returntype functionname(datatype1,..,datatypen);

Ex:
===
int sum(int,int);

Function Calling
====================
Syntax:
======
functionname(arg1,..argn);
Ex:
===
sum(a,b);

Function Defenition
====================
Syntax:
======
returntype functionname(datatype arg1,..,datatype argn)
{
statements
}
Ex:
==
int sum(int x,int y)
{
return x+y;
}

Function Categories
====================

1) With Argument And Return Value
2) With Out Argument And Return Value
3) With Argument and No Return Value
4) No Argument and No Return Value


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printline('*');
printf("\n\t\t Languages\n");
printline('&');
printf("\n\t\t C");
printf("\n\t\t C++");
printf("\n\t\t Java\n");
printline('-');
getch();
}
printline(char c)
{
int i;
for(i=0;i<65;i++)
printf("%c",c);
}


=================


/* Function With Arrays */

#include<stdio.h>
#include<conio.h>
main()
{
int a[5];
clrscr();
a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;
printf("\n Sum is :%d",Sum(a,5));
getch();
}
Sum(int a[],int n)
{
int sum=0,i,res=0;
for(i=0;i<n;i++)
sum=sum+a[i];
return sum;
}

=================


#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printline();
printf("\n\t\t Languages\n");
printline();
printf("\n\t\t C");
printf("\n\t\t C++");
printf("\n\t\t Java\n");
printline();
getch();
}
printline()
{
int i;
for(i=0;i<65;i++)
printf("%c",'*');
}

=====================



#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
printline();
printf("\n\t\t Languages\n");
printline();
printf("\n\t\t C");
printf("\n\t\t C++");
printf("\n\t\t Java\n");
printline();
getch();
}
printline()
{
int i;
for(i=0;i<65;i++)
printf("%c",'*');
}

=====================


#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
clrscr();
printf("\n Enter Value For a\n");
scanf("%d",&a);
printf("\n Enter Value For b\n");
scanf("%d",&b);
printf("\n Addition is :%d",sum(a,b));
getch();
}
int sum(int x,int y)
{
return x+y;
}

===================






No comments:

Post a Comment