Friday 2 November 2012

3 type of FUNCTION (c prgramming)

Below are 3 types of function that usually been use for create a program in programming lesson and i also provide the example of each function...ENjoy YA...
1. Functions with no arguments and no return values
/* Program to illustrate a function with no argument and no return values*/
#include

void statement1();
void starline();
void statement2();


int main()
{
statement1();
starline();
statement2();
starline();
return 0;
}
/*function to print a message*/
void statement1()
{
printf("\n Sample subprogram output\n");
}
void statement2()
{
printf("\n Sample subprogram output two\n");
}
void starline()
{
int a;
for (a=1;a<60;a++)
printf("%c",'*');
printf("\n");
}


2. Functions with arguments but no return values

/*Program to find the largest of two numbers using function*/

#include
void largest(int, int);
int main()
{
int a,b;
printf("Enter the two numbers");
scanf("%d%d",&a,&b);
largest(a,b);
return 0;
}
/*Function to find the largest of two numbers*/
void largest(int a, int b)
{
if(a>b)
printf("Largest element=%d",a);
else
printf("Largest element=%d",b);
}

3. Functions with arguments and return values
#include
float add(float,float);
double sub(double,double);
int main()
{
float x=12.0;
float y=9.0;
printf("%f\n",add(x,y));
printf("%lf\n",sub(x,y));
return 0;
}
float add(float a,float b)
{
return (a+b);
}
double sub(double p,double q)
{
return(p-q);
}

No comments:

Post a Comment