# Making functions

This is a discussion on Making functions within the C Programming forums, part of the General Programming Boards category; I am currently studying C programming as part of my curriculum. I'm over half the way through the quarter and ...

1. ## Making functions

I am currently studying C programming as part of my curriculum. I'm over half the way through the quarter and I have an A in the class, but I've hit a stone wall with this week's homework. This is what I have to do:

Write a C function named change() that accepts a single-precision number and the addresses of the integer variables named quarters, dimes, nickels, and pennies. The function should determine the number of quarters, dimes, nickels, and pennies in the number passed to it and write these values directly into the respective variables declared in its calling function.

I am not asking for people to make this for me, I only ask for help in understanding it, at this point I don't even know where to begin.

2. Do you understand all the sentences in the problem description? If not, which ones do you not?

Do you know how to make change?

3. I know that I have to convert an amount into number of quarters, dimes, nickels and pennies. I'm just confused on functions as a whole.

4. So what part of "the whole idea of functions" DO you know, and what particular part are you struggling with?

Can you get started on a function that does what you need here?

--
Mats

5. I don't know how to make a function, I've looked at examples in our books and could point out a function, but I haven't a clue on how to make it. Maybe you guys could give me an example with a different conversion of numbers; something like feet to inches and then I could piece it together from there?

6. There's nothing more to making a function than what you've got. Figure out what to do, and put it there. For instance, to convert inches to feet, you divide by twelve, hence:
Code:
```float inches_to_feet(float inches) {
return inches/12.0f;
}```
You put the code that you want to happen in the function, and you're done.

7. Have a look at http://www.cprogramming.com/tutorial/c/lesson4.html

Then give it a try - you can not learn how to ACTUALLY do programming without actively writing your own code - that would be like trying to learn how to write a book by reading good books.

--
Mats

8. thanks for the link, I'll take a look.

9. I don't know if this is of any help - it depends on what you already know.

This is a function called "printTheNumberFive". It simply prints out some words, then finishes. It "returns" nothing so it's type is 'void' - as in void printTheNumberFive. We don't 'pass' anything to the function so the brackets after the function name is empty. Before you can call the function, you need to declare and then define it.

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

// 1. declare the function
void printTheNumberFive();

int main (int argc, const char * argv[]) {
//3. Call the function
printTheNumberFive();
return 0;
}

//2. define the function
void printTheNumberFive() {
printf("\nThe number five");
}```
This is a function which returns an integer, so it's type is 'int' as in int returnTheNumberFive. As before we do not 'pass' anything to the function, so again the brackets after the name are empty

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

// 1. declare the function
int returnTheNumberFive();

int main (int argc, const char * argv[]) {
//3. Call the function
int number = returnTheNumberFive();
printf("\nThe number returned was %d", number);
return 0;
}

//2. define the function
int returnTheNumberFive() {
return 5;
}```

Finally, here is a function which also returns an integer, so its type is also int. Unlike the previous functions, this one is designed to take a number (an integer to be exact). Therefore the brackets after the function name now contain the words (int aNumber).

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

// 1. declare the function

int main (int argc, const char * argv[]) {
//3. Call the function (this time passing the number 6 to it)
printf("\nThe number returned was %d", number);
return 0;
}

//2. define the function
int newNumber = aNumber + 1;
return newNumber;
}```
Is this too simple or too complicated?

10. ^ is that an assignment?

11. Which '^' did you mean master5001? The OP's post or the officedog post?

12. You.

13. Nope. Not an assignment. Wrote it on the spot in attempt to help out.

14. Oh ok. I was going to say man your instructor really doesn't worry about insulting the intelligence of the class.

15. I did some studying and this is what I came up with. I am not sure what kind of setup I should have to find the remaining dimes, nickels and pennies. Please tell me if there is anything else wrong with the code, thanks.

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

void change(double, double *, double *, double *, double *);
double amount, quarters, dimes, nickels, pennies;

int main()
{
printf("Enter a dollar amount.");
scanf("%f", &amount);

change(amount, &quarters, &dimes, &nickels, &pennies);

printf("\nThe amount of quarters is: %6.2d\n", quarters);
printf("\nThe amount of dimes is: %6.2d\n", dimes);
printf("\nThe amount of nickels is: %6.2d\n", nickels);
printf("\nThe amount of pennies is: %6.2d\n", pennies);

return 0;
}

void change(double total, double *quarter, double *dime, double *nickel, double *penny)
{
*quart = total / .25;
*dime =```

Page 1 of 2 12 Last