Thread: Quick Question about Homework

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    70

    Quick Question about Homework

    Ok, so I had an assignment due a few days ago and I typed it all up myself without any help (pretty proud of myself at the time), but my instructor took off 4 out of 20 points because I didn't make it a void function.

    Here is the code:

    Code:
    #include <stdio.h>
    #define MAXARRAY 10
    
    double extend(double[], double[], double[], int);
    
    int main(){
    
        double price[MAXARRAY] = {10.62, 14.89, 13.21, 16.55, 19.62, 9.47, 6.58, 19.32, 12.15, 3.99};
        double quantity[MAXARRAY] = {4, 9.5, 6, 7.35, 9, 15.3, 3, 5.4, 2.9, 4.9};
        double amount[MAXARRAY];
        int i;
        
        extend(price, quantity, amount, MAXARRAY);
        
        for (i = 0; i < MAXARRAY; i++)
            printf("The product of price %d and quantity %d is amount %5.2f\n", i+1, i+1, amount[i]);
    
        return 0;
    }
    
    double extend(double price[], double quantity[], double amount[], int max){
    
        int i;
        double product;
        double *p, *q;
    
        p = price; q = quantity;
    
        for (i = 0; i < max; i++)
        
            amount[i] = *p++ * *q++;
            amount[i] = product;
        
            return (product);
    }
    The assignment doesn't say "extend" needed to be a void function. All it said is that the function "extend" needed to have three arrays passed to it.

    This is what my instructor said:

    extend should be a void function. You even call it as a void function! What do you think the variable product is doing in the function extend?
    Not trying to call him out or anything, but just wondering why extend() needs to be a void function. The program works fine the way it is. It displays the product of the two arrays for each element just like the program asks.

    Is it just sloppy coding?

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Yes, it'll work fine, but there's no reason for a return value because you're not assigning the return value of extend() to anything in main(). Since you're not using the return value, just make extend() a void.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by xwielder View Post
    Yes, it'll work fine, but there's no reason for a return value because you're not assigning the return value of extend() to anything in main(). Since you're not using the return value, just make extend() a void.
    Oh ok, so since extend() acts like a void function it would of been proper to call it a void function. I believe what I was thinking is that it needed to be called "double" since the array amount[] was being assigned double values, and I thought anything other than void needed a return value other than "0". Just a rookie mistake on my part, but I didn't think it was worth an 80% grade

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your instructor is correct. Your program doesn't "catch' the value it returns, and there's no sense in returning anything from extend, in any case.

    It's like in football - making a great pass is fine, but it's useless to make the pass, unless a teammate is there to receive it.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I would mark you 80% just for the dubious use of product.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    70
    Quote Originally Posted by Adak View Post
    Your instructor is correct. Your program doesn't "catch' the value it returns, and there's no sense in returning anything from extend, in any case.

    It's like in football - making a great pass is fine, but it's useless to make the pass, unless a teammate is there to receive it.
    Heh, yea. Good analogy. Just sloppy work on my part. Should of done a better job at "proof reading" my code before submitting it just because it worked.

  7. #7
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Quote Originally Posted by Cameron Taylor View Post
    ...and I thought anything other than void needed a return value other than "0"...
    No, a void() function can simply "return;", or doesn't even need a "return;" at all. Anything OTHER than a void() function, needs to return whatever you're casting the function as.

    Fore example:
    Code:
    // MUST return an int value
    int functionOne(int a, int b)
    {
         int c = a + b;
         return (c);
    }
    
    // Doesn't need to return anything
    void functionTwo(int a, int b)
    {
         int c = a + b;
         return;
    }
    
    // Doesn't need to return anything
    void functionThree(int a, int b)
    {
         int c = a + b;
    }
    
    // This is not correct.  A void() function cannot return any value at all
    void functionFour(int a, int b)
    {
         int c = a + b;
         return (c);
    }
    
    // This is not correct.  You MUST return the same type as the casted function.
    char *functionFive(int a, int b)
    {
         int c = a + b;
         return (c);
    }
    and so on and so on...

    Hope this helps.
    Last edited by xwielder; 07-22-2013 at 12:27 AM. Reason: small typo

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    void()
    Parentheses?

    Paretheses have 3 meanings in C: order of operations, casting, and function invocation. You cannot have a function named void, and it is very confusing to think of the return value as or as part of the name.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Quote Originally Posted by whiteflags View Post
    Parentheses?

    Paretheses have 3 meanings in C: order of operations, casting, and function invocation. You cannot have a function named void, and it is very confusing to think of the return value as or as part of the name.
    Parentheses weren't mean as a literal, they were there for easier readability. You can do this though
    Code:
    void someFunction(void)
    {
         int c = 123;
    }
    But you are correct, "void()" is invalid in application.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question in cin(homework)
    By cipher1285 in forum C++ Programming
    Replies: 1
    Last Post: 03-13-2013, 10:41 AM
  2. Homework Question
    By Jozrael in forum C++ Programming
    Replies: 7
    Last Post: 02-17-2008, 05:42 AM
  3. Homework question
    By Jozrael in forum C++ Programming
    Replies: 7
    Last Post: 02-07-2008, 09:30 PM
  4. Another Homework Question
    By Trekkie in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2005, 06:57 AM
  5. I have a homework question?
    By correlcj in forum C++ Programming
    Replies: 4
    Last Post: 10-03-2002, 10:38 PM