case switch

I'm working on an assignment and I was wondering if you can call functions in case switches? Maybe a dumb questions forgive me. I have a question about overload functions but I think I should one question at a time for my brain. lol

Yes and yes.

And they are better called "switch cases" instead of "case switches".
Thank you, and after each case, its a good idea to use a break. Is that correct?
Absolutely. You pretty much always want to use a break at the end of a case.
You can use any function in the control expression of the switch:

1
2
3
4
switch (f(x))
{
  ...
}

However, the branch expressions must have constant value. Hence, you may use literals, const values, or constexpr expressions (which include constexpr functions).


The nature of your question, however, leads me to think you are a little confused about the purpose of a switch statement. It is simply a convenience form to reduce a bunch of if...else if... statements where the only thing that changes is the value being compared. Compare:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Harder to read and maintain
if (x == 1)
{
  ...
}
else if (x == 2)
{
  ...
}
else if (x == 3)
{
  ...
}
else
{
  ...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Easier to read and maintain
switch (x)
{
  case 1:
    ...
    break;

  case 2:
    ...
    break;

  case 3:
    ...
    break;

  default:
    ...
}

Essentially, it is a branch-lookup device built into C and C++ syntax.
More reading at Wikipedia: https://en.wikipedia.org/wiki/Switch_statement

Hope this helps.
Last edited on
I was assuming you meant calling functions within the case code, not the case test. As Duthomhas says, the actual case tests need to be constant (they can't even be a variable).
It does, I forgot about that default. I need to figure out what my default should be lol. I have to do a couple of overload functions too lol. I'm stuck on that too. I get the basic concept but you never really have to do basic ones when you are doing an assignment lol.

For example I'm trying make an overload function dealing with pay salary. the user must enter their information as far as hours and what not. I know the formula to get their salary, but I don't know how to execute it in an overload functions lol.

@dutch, I was meaning calling functions inside the case. Lets say for example my function I want to call is GetData(). I'm terrible at examples

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// Easier to read and maintain
switch (x)
{
  case 1:
    ...GetData(); //Could you do that?
    break;

  case 2:
    ...
    break;

  case 3:
    ...
    break;

  default:
    ...
}

Yes, you can definitely use a function there.
ok thank you. This is really helping. Its so hard to get help with this stuff. Obviously you want to learn, but its hard not just giving the answer when it comes to coding lol. So if you are trying to do an overload function. I'm trying to do something with salary. Like what ever the user inputs for amount and I would divide it by 52 which is 52 weeks. I get the logic and the math, but its different when you are trying to make it into an overload function. lol
oh could you call a function inside an overload function? lol. That help in my overload functions
> could you call a function inside an overload function?

Yes.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

// overloaded function with 2 args
double max_of( int a, int b ) { return a<b ? b : a ; }

// overloaded function with 3 args
int max_of( int a, int b, int c )
{
    // call overloaded function with 2 args (twice)
    return max_of( a, max_of(b,c) ) ;
}

// overloaded function with 4 args
int max_of( int a, int b, int c, int d )
{
    // call overloaded function with 3 args, then overloaded function with 2 args
    return max_of( a, max_of(b,c,d) ) ;
}

int main()
{
    std::cout << max_of( 23, 45, 42, 17 ) << '\n' ; // 45
}

http://coliru.stacked-crooked.com/a/a3c4e2fa3c8056d5
is it the only purpose for an overload function to pass different type of variables? I making a salary calculation, so i just made everything a double variable.

1
2
3
4

void getEmployeesSalary(double &); //this is for the employee to enter the amount 
                                                          //I have seven employees
                                                         


I am required to make an overload function. So I was just thinking of calling the function getEmployeesSalary(); inside the overload function and some how make it : getEmployeesSalary() and divide it by 52. lol. Guys, I'm probably extremely off here.
Can you post the full assignment? I think we could give you more useful info if you do. I'm wondering if you're supposed to overload the function or override it. Those are two very different things in C++.

And another small thing, for you getEmployeeSalary(), it would probably be better as double getEmployeeSalary();. That way you can put it inside an expression.
you mean like my instructions?
i had it like my teacher had some examples

I had it like this: void getEmployeeSalary(double &);
I think you are overthinking this.

Yes, post your instructions and we can give you very straightforward advice that will help simplify your thoughts.
Topic archived. No new replies allowed.