Monday, 23 February 2015

Functions in C


Function Call
Function definition   
Function declaration

#include<stdio.h>
#include<conio.h>

void main()
{
int addnum(int,int);
int sum,a,b;
printf(“Enter two number:”);
scanf(“%d%d”,&a,&b);
sum=addnum(a,b);
printf(“The sum of %d and %d is %d”,a,b,sum);
getch();
}
int addnum(num1,num2)
{
int tot;
tot=num1+num2;
return(tot);
}

#include<stdio.h>
#include<conio.h>

void main()
{
char lower,upper;
clrscr();
printf(“Enter a lower-case character:”);
scanf(“%c”,&lower);
upper=lower_to_upper(lower);
printf(“The upper case equivalent is %c\n\n”,upper);
getch();
}
char lower_to_upper(char c1)
{
char c2;
c2=(c1>=’a’ && c1<=’z’)?(c1-32):c1;
return(c2);
}

#include<stdio.h>
#include<conio.h>

void main()
{
char lower,upper;
clrscr();
printf(“Enter a upper-case character:”);
scanf(“%c”,&upper);
lower=upper_to_lower(upper);
printf(“The lower case equivalent is %c\n\n”,lower);
getch();
}
char upper_to_lower(char c1)
{
char c2;
c2=(c1>=’A’ && c1<=’Z’)?(c1+32):c1;
return(c2);
}

Function Reference

#include<stdio.h>
#include<conio.h>
void main()
{
int maximum(int,int);
int a,b,c,d;
clrscr();
printf(“\n a=”);
scanf(“%d”,&a);
printf(“\n b=”);
scanf(“%d”,&b);
printf(“\n c=”);
scanf(“%d”,&c);
d=maximum(a,b);
printf(“\n Maximum=%d”,maximum(c,d));
getch();
}
int maximum(int x,int y)
{
int z;        
z=(x>=y)?x:y;
return(z);
}

Function without argument and with return value:

#include<stdio.h>
#include<conio.h>
void main()
{
int add();
int r;
clrscr();
r=add();
printf(“%d”,r);
printf(“%d”,add());
getch();
}
int add()
{
int a,b,c;
printf(“\n Enter two numbers:”);
scanf(“%d%d”,&a,&b);
c=a+b;
return ( c );
}

Function with argument and with return value

#include<stdio.h>
#include<conio.h>
void main()
{
int add(int,int);
int a,b,r;
clrscr();
printf(“\n Enter two numbers:”);
scanf(“%d%d”,&a,&b);
r=add(a,b);
printf(“%d”,r);
getch();
}
int add(int x, int y)
{
int c;
c=x+y;
return ( c );
}

No comments:

Post a Comment