简体   繁体   中英

Calling a function within a switch case

I have been trying to create a calculator or a semi calculator, I want to know what went wrong with my code and why?

This is my code where I get errors while compiling it, in one machine I get different error while in other machine, the error is different.

I hope you guys can help me sort it out. Its a small program to understand C programming little bit better.

#include<stdio.h>
int main()
{
    int operation,fc,v1,v2;
    double fcd;
    printf("input 2 values\n");
    scanf("%d%d",&v1,&v2);
    /* telling the user to choose any type of calculator */
      printf("please choose what you want to do with your values\n");
      printf("1- Sum\n");
      printf("2- substracttion\n"); 
      printf("3- multiplay\n");
      printf("4- devision\n"); 
      scanf("%d",&operation);//input a
      switch(operation)
    {
      case 1:
      int fc = sum(int v1, int v2);
        printf("sum of two values = %d\n",fc);
        break;

    case 2:
       int fc = substact(int v1, int v2);
       printf("substract of two values = %d",fc);
        break;

    case 3:
        int fc = multiplay(int v1, int v2);
        printf("multiply of two values = %d\n",fc);
        break;

    case 4:
       int fcd = devision(int v1, int v2);
       printf("division of two values = %d\n",fc);
        break;
    default:
        printf("wrong choice\n");
    }
return 0;}

int sum(int a,int b)
{
    int sum=0;
    um=a+b;
    return sum;
}
int substact(int a,int b)
{
    int sub=0;
    sub=a-b;
    return sub;
}
int multiplay(int a,int b)
{
    int mult=1;
    mult=a*b;
    return mult;
}
double devision(int a,int b)
{
    double  devi=1;
    devi=a/b;
    return devi;
}

Problem 1
C (pre C99) doesn't allow the declaration of variables in local scope anywhere except start of block. (Before any statement is executed). Also in your case it can cause multiple variables with same name in same scope which is not allowed.

int fc and int fcd break above condition so should be moved to the top of function. (They are already present, just remove int from cases)

Problem 2
While passing arguments, you don't need to give the type. For example sum(int v1, int v2); is bad. (Remove int from the arguments)

Warning
Always declare (or define) the functions before their first usage. In your program, functions like sum, substract etc are used before declaration in main .

Further readings:
Variable inside switch
function call in C
Function in C
Why do functions need to be declared before they are used?

The below statement in your code might have caused compilation errors for you.

 int fc = sum(int v1, int v2);

You have already declared fc variable then why are you declaring it here again. Declaring variables multiple times within a function can cause compilation errors. One more thing is that you cannot make a call to a function in the below way int fc = sum(int v1, int v2). Go through books related to C programming on how to make a funcction call.

SO modify the above C statement as follows.

       fc = sum(v1,v2);

I have not compiled your code but i think that above change should resolve your compilation errors

Compilation error is most likely about declarations of functions sum, substract etc. Code you have written have defined the functions after they have been used in the main function. Please "declare" all functions BEFORE main function, if you plan to call them from main function.

I hope this solution will help you. Before you call the function, you must declare first. Here is sample source code.

   #include <stdio.h>

  /* function declaration */
  int max(int num1, int num2);

 int main () {

 /* local variable definition */
 int a = 100;
 int b = 200;
 int ret;

 /* calling a function to get max value */
 ret = max(a, b);

 printf( "Max value is : %d\n", ret );

return 0;

}

 /* function returning the max between two numbers */
 int max(int num1, int num2) {

 /* local variable declaration */
int result;

if (num1 > num2)
  result = num1;
else
  result = num2;

return result; 

}

Several Issues:

You need to declare functions before you use them.

You can not repeat the type declaration. for example, you declare fc at the beginning of your program, and you repeat it the switch statement cases: "int fc =...", just use "fc =..."

Last, do not declare the type when calling a function. below is a minimally corrected code:

#include<stdio.h>
int sum(int a,int b);
int substact(int a,int b);
int multiplay(int a,int b);
double devision(int a,int b);

int main()
{
    int operation,fc,v1,v2;
    double fcd;
    printf("input 2 values\n");
    scanf("%d%d",&v1,&v2);
      /* telling the user to choose any type of calculator */
    printf("please choose what you want to do with your values\n");
    printf("1- Sum\n");
    printf("2- substracttion\n"); 
    printf("3- multiplay\n");
    printf("4- devision\n"); 
    scanf("%d",&operation);//input a
    switch(operation)
    {
      case 1:
        fc = sum(v1, v2);
        printf("sum of two values = %d\n",fc);
        break;

      case 2:
        fc = substact(v1, v2);
        printf("substract of two values = %d",fc);
        break;

      case 3:
        fc = multiplay(v1, v2);
        printf("multiply of two values = %d\n",fc);
        break;

      case 4:
        fcd = devision(v1, v2);
        printf("division of two values = %d\n",fc);
        break;
      default:
        printf("wrong choice\n");
    }
return 0;
}

int sum(int a,int b)
    {
    int sum=0;
    sum=a+b;
    return sum;
    }
int substact(int a,int b)
    {
    int sub=0;
    sub=a-b;
    return sub;
    }
int multiplay(int a,int b)
    {
    int mult=1;
    mult=a*b;
    return mult;
    }
double devision(int a,int b)
    {
        double  devi=1;
    devi=a/b;
    return devi;
    }

This is another little bit different program and easy way to understand the calling a function within a switch case. This is one of the easiest ways to declare and call the function

Declare all the functions and call from the outside of a function

#include<stdio.h>
#include<conio.h>
int x,y;
int addi(int,int);//when call the function. The function must be integer type.
int multi(int,int);
int divi(int,int);
int square(int);
int evenOdd(int);
int substracn(int,int);

int main()
{
   int choice;   

printf("*********************************************************************\n");
printf("\t\tCalculator\n");
printf("*********************************************************************\n");

printf("Enter a choice what you want to perform");
printf("\n\t1.Addition");
printf("\n\t2.Multiplication");
printf("\n\t3.Division");


printf("\n\t4.Square");
printf("\n\t5.Even or Odd");
printf("\n\t6.Substraction\n");
scanf("%d",&choice);



switch(choice)//switch case use when more than 2 options are there. Then it is the best 
{
case 1: printf("\n\tAddition");
        printf("\nEnter two no for addition");
        scanf("\n%d\t%d",&x,&y);
        addi(x,y);//function by passing paramenter
        break;//break the statement after the execution of the function definition

case 2: printf("Enter two no's for multiplication");//multiplication
        scanf("\n%d\t%d",&x,&y);
        multi(x,y);
        break;

case 3: printf("Enter two no's for division");
        scanf("%d%d",&x,&y);
        divi(x,y);
        break;


case 4: printf("Enter one no for square of no");
        scanf("%d",&x);
        square(x);
        break;

case 5: printf("Enter the no for even odd");
        scanf("%d",&x);
        evenOdd(x);
        break;

case 6: printf("Enter the two no's for Substraction");
        scanf("%d%d",&x,&y);
        subtracn(x,y);
        break;
default:
    printf("Enter a valid option");

}

getch();
}

int addi(int a,int b)
{
    int c;
    c=a+b;
    printf("sum of no %d + %d is: %d",a,b,c);

}

int multi(int a,int b)
{
    int c;
    c=a*b;
    printf("multiplication of no %d x %d is: %d",a,b,c);

}

int divi(int a,int b)
{
    int c;
    c=a/b;
    printf("division of no's is: %d",c);

}

int square(int a)
{
    int c;
    c=a*a;
    printf("square of no %d is: %d",a,c);

}

int evenOdd(int a)
{
    if(a%2==0)
    {
    printf("The no is even : %d",a);
    }
    else
    {
    printf("The no is odd : %d",a);    
    }

}
int subtracn(int a,int b)
{
    int c;
    c=a-b;
    printf("Substraction of no's is: %d",c);

}`
    let role = "teacher";
    
    forStudent = ()=>{
        console.log("from student");
    }

    forTeacher = ()=>{
        console.log("from teacher");
    }

    forOwner = ()=>{
        console.log("from owner");
    }
    
    defFunc = ()=>{
        console.log("its default");
    }

    switch(role){
        case 'student': forStudent();
        break;
        
        case 'teacher': forTeacher();
        break;
        
        case 'owner': forOwner();
        break;
        
        default: defFunc ();
    }

this is how simple it is.

Make sure your function is defined before calling it and it's done

The above code is in Javascript but hope it makes sense for else languages as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM