Thread: Making functions

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    23

    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. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    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. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    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. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    thanks for the link, I'll take a look.

  9. #9
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    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 addOneToANumber(int aNumber);  
    
    int main (int argc, const char * argv[]) {
        //3. Call the function (this time passing the number 6 to it)
        int number = addOneToANumber(6); 
        printf("\nThe number returned was %d", number);
        return 0;
    }
    
    //2. define the function
    int addOneToANumber(int aNumber) {
        int newNumber = aNumber + 1;
        return newNumber;
    }
    Is this too simple or too complicated?

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    ^ is that an assignment?

  11. #11
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Which '^' did you mean master5001? The OP's post or the officedog post?

  12. #12
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You.

  13. #13
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Nope. Not an assignment. Wrote it on the spot in attempt to help out.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Oh ok. I was going to say man your instructor really doesn't worry about insulting the intelligence of the class.

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    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 =

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM
  3. DLL class member functions
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2003, 06:59 AM
  4. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  5. Passing data/pointers between functions #2
    By TankCDR in forum C Programming
    Replies: 1
    Last Post: 11-02-2001, 09:49 PM