Friday 28 February 2014

Explain types of function with example.


Types of function

1)without arguments and return values-Here neither the data is passed from calling function nor the data is sent back from called function.
The function is executed,they print result in same block.Such type is used for printing messages,drwa a line or split line etc.

Write an example of function without argument and return values.

#include<stdio.h>
#include<conio.h>
void main()
{
    void a(),b(),c();
    a();   //Here main() function calls 3 functions//
    b();
    c();
}

void a()
{
    printf("\n A");
}
void b()
{
    printf("\n B");
}
void c()
{
    printf("\n C");
}
Output
ABC

2)with arguments but without return values-The arguments are passed to the called function,but no result is sent back.

write an example of function with arguments and no return values.

#include<stdio.h>
#include<conio.h>
main()
{
    int datee(int,int,int);
    int d,m,y;
    clrscr();
    printf("Enter date:");
    scanf("%d \t %d \t %d",&d,&m,&y);
    datee(d,m,y);
    return 0;
}
datee(int x,int y,int z)
{
    printf("Date=%d/%d/%d",x,y,z);
}
Output
Enter date:11 2 2013
Date=11/2/2013

3)with arguments and return values-The arguments are passed to called fucntion and result is sent back to calling function.

write an example of function with arguments and return value.

#include<stdio.h>
#include<conio.h>
main()
{
    int datee(int,int,int);
    int d,m,y;
    clrscr();
    printf("Enter date:");
    scanf("%d \t %d \t %d",&d,&m,&y);
    t=datee(d,m,y);
    printf("Tomorrow=%d/%d/%d",t,m,y);
    return 0;
}
datee(int x,int y,int z)
{
    printf("Today=%d/%d/%d",x,y,z);
    return(++x);
}
Output
Enter date: 12 2 2013
Today=12/2/2013
Tomorrow=13/2/2013

4)without arguments and with return values-No arguments are passed to called function,but called function returns values.
It reads values from keyboard.

write an example of function without arguments and with return values.

#include<stdio.h>
#include<conio.h>
main()
{
    int sum,s
    clrscr();
    s=sum();
    printf("\n Sum=%d",s);
}
sum()
{
    int x,y,z;
    printf("\n Enter 3 values:");
    scanf("%d %d %d",&x,&y,&z);
    return (x+y+z);
}
Output
Enter 3 values: 3 4 5
Sum=12

No comments:

Post a Comment