-7

Is it possible to put a function inside a switch case loop? Because I've tried this just to explore more of this loop. Though I tried other ways but still I've got the problem existed. Can anyone help me?

#include <stdio.h>
int main(void)

{

    int choice;

    switch(choice);
    {
    case 1:
    {
           int GetData()
           {
           int num;
           printf("Enter the amount of change: ");
           scanf("%d%*c", &num);

           return (num);

           }
           int getChange (int change,int fifty,int twenty,int ten,int five)
           {
                 int num = change;

                 fifty = num/50;
                 num %= 50;
                 twenty = num/20;
                 num %= 20;
                 ten = num/10;
                 num %= 10;
                 five = num/5;
                 num %= 5;

                 return (fifty, twenty, ten, five);
           }
           int main()
           {
                 int change, fifty, twenty, ten, five;

                 change = GetData();

                 if ((change < 5) || (change > 95) || (change%5 > 0))
           {
                 printf("Amount must be between 5 and 95 and be a multiple
                 of 5.");
           }
           else
           {
                 getChange(change, fifty, twenty, ten, five);

                 printf("Change for %d cents: \n", change);

                 if (fifty > 1)
                     printf("%d Fifty cent piece's.\n", fifty);
                 if (fifty == 1)
                     printf("%d Fifty cent piece.\n", fifty);
                 if (twenty > 1)
                     printf("%d Twenty cent piece's.\n", twenty);
                 if (twenty == 1)
                     printf("%d Twenty cent piece.\n", twenty);
                 if (ten > 1)
                     printf("%d Ten cent piece's\n", ten);
                 if (ten == 1)
                     printf("%d Ten cent piece.\n", ten);
                 if (five > 1)
                     printf("%d Five cent piece's\n", five);
                 if (five == 1)
                     printf("%d Five cent piece.\n", five);
           }

          return(0);

         }  

2 Answers2

7

No. Not only is it not possible to define a function inside a switch statement, it's not possible to nest function definitions at all in C. You have to define them all at file scope.

In the code you present, there is no reason at all why you should want or need to define nested functions, so your underlying problem may actually be something different.

In addition:

  • you can't return multiple values from a function like this:

    return (fifty, twenty, ten, five);
    
  • you attempt to define main() twice.

  • you switch on choice before you have initialized choice, which clearly isn't what you want to do.

  • you only have one case in your switch statement, meaning you don't need a switch statement at all.

Crowman
  • 25,242
  • 5
  • 48
  • 56
1

No, you cannot. Functions cannot be generally declared inside another function. You can only define them. The language was designed like that.

Also see these:

I think you need a good C book or tutorial. You can choose a good book from here, or check out this tutorial. I would recommend learning from a book, but stiil if you want to learn online, the tutorial mentioned is good. I myself used to refer to it when I was learning C.

Community
  • 1
  • 1
Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67