Function question

I have this function that should add two numbers if thmethod is 'a'
tmethod is set to a before and cnr1 and cnr two are numbers.
I get a warning like this when i try to compile:
Possibly incorrect assignment in function calc(int,int,char)
Anyone know why?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int calc(int cnr1, int cnr2, char tmethod)
{
  
  
  if (tmethod == 'a')
     calculation = cnr1 + cnr2;
  
  return(calculation);
  
}
// this is how i call the function:
  int int1, int2;        
  char method;
  int ans;
  int1 = 5;
  int2 = 7;
  method ='a';
ans = calc(int1, int2, method);



Last edited on
Could you post more of the code that's before line 12?
I don't think this is the error, but is calculation a global variable?

If yes: There is no need to return the value.

If no: You have to declare it within the function.

And what happens when tmethod is not 'a'? Your function returns a not defined value?
I would hazard a guess that 'a' represents 'addition', so if it did not equal a then a different thing would be called based on what tmethod is. Just a guess.

also
If yes: There is no need to return the value.


if it was called in that way there would be, beacuse then ans would = calculation
It would only not need to be returned if it was called like:

1
2
calc(int1, int2, method);
ans = calculation


and since calc is an int is has to return a value
Topic archived. No new replies allowed.