# Help with functions

This is a discussion on Help with functions within the C Programming forums, part of the General Programming Boards category; This is part of my instructions and what i'm trying to code at the moment... · To accelerate slowly, the ...

1. ## Help with functions

This is part of my instructions and what i'm trying to code at the moment...

· To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.
· To accelerate quickly, the user presses ‘A’. This will increase the speed by 10mph.

// I have not tried reading in the 10mph yet... just the 3mph...

Code:
```#include <stdio.h>
#include <math.h>

int increase(int z, int y)
{

printf("|---------------------|\n");
printf("|   %d                      |\n", total=total+num);
printf("|                             |\n");
printf("|                             |\n");
printf("|---------------------|\n");
return z=z+y;

}

int main(){

int i=0;
int num=3;
int total=0;
char number;

printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
number=getch();

while(i<1){
switch (number)
case 'A':
case 'a':
int increase(total=total+num));
break;
i++;}

getch();
return(0);
}```

2. Code:
```int increase(int z, int y)
{

printf("|---------------------|\n");
printf("|   %d                      |\n", total=total+num); //Ask yourself how this function knows what total and num are?
printf("|                             |\n");
printf("|                             |\n");
printf("|---------------------|\n");
return z=z+y;

int main(){

int i=0;
int num=3;
int total=0;
char number;

printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
number=getch();

while(i<1){
switch (number)
case 'A':
case 'a':
int increase(total=total+num)); //Look at your function definition and compare the parameters with what you are passing on this line. You can also drop the int from the function on this line.
break;
i++;}

getch();
return(0);
}

}```
I suggest you read up on the difference between function definition and actually calling the function.

http://www.cprogramming.com/tutorial/c/lesson4.html

3. first thing is that neither you have declared total and num variables globally nor you have passed it to increase(), second thing is that you have 2 parameters in the increase() function while you are passing only 1 parameter i.e total from main().Third thing is that, in your main() function you have called "int increase(total=total+num)", where int is not needed. And there's no variable to hold the returned value from increase().And no idea why you included math.h.That's all.Please try to rectify these small errors.

4. This is what i have now...

Code:
```#include <stdio.h>

int increase(int total, int num)
{

printf("|---------------------|\n");
printf("|   %d                |\n", total,num);
printf("|                     |\n");
printf("|                     |\n");
printf("|---------------------|\n");

}

int main(){

int i=0;
int num=3;
int total=0;
char number;

printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
number=getch();

while(i<5){
switch (number)
case 'A':
case 'a':
scanf("%d",increase(total,num)); // I'm trying to get total and num to read in for int total and int num in my prototype function... and have my function add 3 mph every time i press the a key...
total=total+num;
break;
i++;}

getch();
return(0);
}```

5. where your increase function does something with its parameters?
Where it returns result for using in the main?

Did you compile this code? you should get at least warnings with decent compiler

6. It doesn't give me any warnings or errors (using dev c++ bloodshed)

i changed up my prototype function to this...
Code:
```int increase(int total, int num)
{

printf("|---------------------|\n");
printf("|   %d                |\n", total,num);
printf("|                     |\n");
printf("|                     |\n");
printf("|---------------------|\n");
return total=total+num; // I'm trying to get my function to add 3mph every time i press the a key... this should do it, right?

}
```

7. You should use the return value to store in your total variable in main.

ex.) total = increase(total, num);

This way the value of total will change. You have your scanf function wrong, technically you do not need the scanf for this to work. What are you planning on doing with scanf?

8. Originally Posted by aron10197
It doesn't give me any warnings or errors (using dev c++ bloodshed)
As I understand dev c++ is IDE and if you are using gcc compiler - you need to increase the warning level

Code:
```gcc -c -o obj/test.o src/test.c -Wall -pedantic -march=core2 -Iinclude -std=c99
src/test.c: In function ‘increase’:
src/test.c:13:3: warning: too many arguments for format [-Wformat-extra-args]
src/test.c: In function ‘main’:
src/test.c:33:1: warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
src/test.c:44:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int’ [-Wformat]
src/test.c: In function ‘increase’:
src/test.c:20:1: warning: control reaches end of non-void function [-Wreturn-type]
gcc -o test obj/test.o -Wall -pedantic -march=core2 -Iinclude -std=c99 -lm
obj/test.o: In function `main':
test.c:(.text+0x82): undefined reference to `getch'
test.c:(.text+0xcf): undefined reference to `getch'
collect2: ld returned 1 exit status
make: *** [test] Error 1```

9. I'm just trying to get my prototype function to increase by 3mph every time... i thought having the scanf func in main would make it repeat the total=total+num assignment operator... scanf("%d");... every time i put in a it would add 3mph... or should it be %c

10. Can you give me an example of how it should look with this instruction:

To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.

11. Pseudo code
Code:
```print(To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.)
while(i<5)
scan number
switch
case a: total = increase (total,num)
..
...```

12. see, i didn't even know you could put anything front of the case a:

Thank you so much!

13. print(To accelerate slowly, the user presses ‘a’. This will increase the speed by 3mph.)
while(i<5)
scan number
switch
case a: total = increase (total,num)

are you saying to put in scanf("%d") for scan number or scan (number);

14. Its just pseudo code for scanf function

15. Code:
```#include <stdio.h>

int increase(int total, int num)
{

printf("|---------------------|\n");
printf("|   %d                |\n", total);
printf("|                     |\n");
printf("|                     |\n");
printf("|---------------------|\n");
return;

}

int main(){

int i=0;
int num=3;
int total=0;
char number;

printf("\nEnter in an a to increase by 3 mph ,or a capital A to increase by 10mph!");
while(i<5){
scanf("%d",&num);
switch (number)
case 'a':total = increase (total,num);
break;
i++;}

getch();
return(0);
}```

Page 1 of 2 12 Last