# Thread: Using functions to convert change into dollars

1. ## Using functions to convert change into dollars

I know this is pretty basic script the problem I have is that I have to use functions for each calculation. The problem I face is that when the user types in the amount of quarters, dimes, nickels, and pennies when a function tries to add them together it displays a huge number instead of the right number.

Here is my code:

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

void insert_money(int p, int n, int d, int q, int total)
{
printf("How much of each coin do you want to insert: \n");
printf("Quarters: ");
scanf("%d", &q);

printf("Dimes: ");
scanf("%d", &d);

printf("Nickels: ");
scanf("%d", &n);

printf("Pennies: ");
scanf("%d", &p);
}

void return_dollars(int p, int n, int d, int q, int dollars, int total)
{
total = 25*q + 10*d + 5*n + p;
dollars = total/100;
total = p % 100;

printf("\nYou have this many Dollars: %d\n", dollars);
}

void remove_money(int p, int n, int d, int q, int total)
{
q = total/25;
total = p % 25;

d = total/10;
total = p % 10;

n = total/5;
total = n % 5;
}

void display(int p, int n, int d, int q)
{
printf("You have this many Quarters: %d\n", q);
printf("You have this many Dimes: %d\n", d);
printf("You have this many Nickels: %d\n", n);
printf("You have this many Pennies: %d\n", p);
}

void main()
{
int p, n, d, q, dollars, total;

insert_money(p, n, d, q, total);

return_dollars(p, n, d, q, dollars, total);

remove_money(p, n, d, q, total);

display(p, n, d, q);

}```

2. First things first. //Welcome to the forum!!

We use to write
Code:
```int main(void)
{
...
return 0;
}```
instead of the void main() you have there.

Now, an piece of advice, when you get large numbers and you expect to get some regular numbers, then probably, a logical error has occur, of type uninitialized variable!

Let me see your code now...Hmm...

What's up with the passing of the arguments in your functions? Are they passed by value or by reference? Read your notes and try again

3. That is a good question, but when I change the variables instead of them being the same as the arguments it will tell me that I have 'undeclared variables'.

4. That's not the case. Haven't you have been shown a swap example?

You need to pass the arguments by reference. Recall, that you can always return a variable, or return something, but this is just one "thing". For example, this code returns a value, to inform main.

However, you have more than one variables, so you should use "passing arguments by reference", because this is what you are supposed to be practicing right now.
Check this example and then try again

5. My latest assignment was to use pointers I am just trying to redo an older assignment because I am terrible with functions and I know they are important and basic.

What is the difference then of pointers and passing arguments by reference?

Edit: How can pass the information the user typed in the first function into the second function and so on?

6. you can consider the relative merits by looking these things up

7. What is the difference then of pointers and passing arguments by reference?
Passing by reference is accomplished by using pointers in C. This code changes 42:
Code:
```#include <stdio.h>

void foo(int *x) {

*x += 1;

}
int main(void) {
int y = 41;

foo(&y);

printf("%d\n", y);

return 0;
}```
You can use the parameter list to change multiple variables. Whenever you want to change a variable of some type, even another pointer,you need to use a pointer -- add a *.

If you're not allowed to do that -- you did mention this is an earlier assignment -- you should make a struct type and use that. Make sure your function has a return value if you do it that way.
Code:
`struct money foo(struct money myMoney);`

8. Originally Posted by std10093
Check this example
Originally Posted by letsrelax
..I am terrible with functions and I know they are important and basic.
Edit: How can pass the information the user typed in the first function into the second function and so on?
Did you really read the example I provided?