How to pass different data type as parameter in C?

 Return different data type in the same function in c:

  • If passing different datatype then must be declared in global.
  • Similarly If needs to return different data type in the same function then must be given function return type as char.
  • When you give the char data type should use the pointer concept.

Example program for print the set of elements:

  1. #include<stdio.h>

    #include<conio.h>

    float calc(int, float); //calc function declaration

    float discount(float); // discount function declaration

    char *display(int,float,char []);//display function declaration

    int main()

    {

              int qty;

              float result,price;

              char name[15];

              clrscr();

              printf("Enter a product name :");

              scanf("%s",&name);

              printf("Enter a qty:");

    scanf("%d",&qty);

    printf("Enter a price:");

    scanf("%f",&price);

    display(qty,price,name); //display function calling

    result=calc(qty,price);//calc function calling

    printf("\nThe total amount is %f",result);

    discount(result); // discount function calling

    getch();

    return 0;

    }

    float calc(int x,float y) //calc function definition

    {

    float z;

    z=x*y;

    return z;

    }

    float discount(float a) // discount function definition

    {

    if(a>=500)

    {

    a=a*0.30;

    printf("\n 30 percent discount so paid amount is : %f",a);

    return a;

    }

    else if(a<=499&&a>=300)

    {

    a=a*0.20;

    printf("\n20 percent discount so paid amount is : %f",a);

    return a;

    }

    else if(a<=299&&a>=100)

    {

    a=a*0.10;

    printf("\n 10%discount so paid amount is : %f",a);

    return a;

    }

    else{

    a=a*0.05;

    printf("\n 5 percent discount so paid amount is : %f",a);

    return a;

    }

    }

    char *display(int q,float p,char *n) //display function definition

    {

    printf("\nDetails of product.....");

    printf("\nItem name : %s",n);

    printf("\nQuantity of item : %d",q);

    printf("\nUnit price is : %f",p);

    return "null";

    }

Output:

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Service Support

Need our help to Learn or Post New Concepts Contact me

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me