Thread: Function prototype

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    Function prototype

    I wrote my code as below:





    But now I want to make it as Define a function void search_course(course arr[], int length) which searches the array of courses for the input department and course number. Make sure to use the string library when comparing strings such as the department.

    The code works fine, but I am not sure how to create a function for search and then call it in main().

    any help would be great!
    Last edited by Lina_inverse; 10-06-2012 at 05:02 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    In generally we use function like this
    <return value> <function name>(<arguments>)

    If no return value use void.
    If no arguments use void.
    Code:
    int myFunction(int x); /*declaration*/
    
    int main(void)
    {
           int x = 5;
           x = myFunction(x);
           /*x is 7 now*/
           return 0;
    }
    
    int myFunction(int x)
    {
         x = 7;
    }
    So get from the main the part of code you do the search and write it into the body of the function .Try to think what return value the function should have and what arguments

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Your prototype for search_course is incomplete. You need to also pass the department name and course number.
    Also returning no information from the function (void) doesn't seem very useful. Maybe it's better to return the index of the searched item or -1 if not found. Your prototype would then be
    Code:
    int search_course(const course arr[], int length, const char *depname, int courseno);

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by std10093 View Post
    Code:
    int myFunction(int x)
    {
         x = 7;
    }
    I think you forgot a return x; in the definition of myFunction().

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Above main(), but after the definition of course, add a declaration
    Code:
     void search_course(course arr[], int length);
    Anywhere (except before the declaration of search_course(), and not inside another function body or type definition) define that function.
    Code:
    void search_course(course arr[], int length)
    {
         /*    Move whatever code needed into here to implement the search_course() behaviour */
    }
    Note that I'm assuming you want this function in the same source file as main(). This definition can even be below the main() function.

    Lastly you need to include a statement in main() to call the function. For example;
    Code:
    int main()
    {
      course a[8] = {
        {"ANTH", 395, 3, "Work, Technology, and Society An IT Perspective"},
        {"CDS", 130, 3, "Computing for Scientists"},
        {"CHEM", 350, 3, "Computer Techniques for Chemistry"},
        {"CEIE", 117, 3, "Information Technology for Engineering"},
        {"GOVT", 300, 4, "Research Methods and Analysis"},
        {"HIST", 390, 3, "The Digital Past"},
        {"IT", 103, 3, "Introduction to Computing"},
        {"MUSI", 415, 3, "Music in Computer Technology"}
      };
    
       search_course(a, 8);
    }
    If you have other arrays you want to call search_course() for, then there is nothing preventing you adding additional lines to main(), like
    Code:
        search_course(b, 9);    /* Assuming we have another array of course named b, with 9 elements */
    The declaration of search_course() above main() helps the compiler know what search_course() is ..... the compiler will not look ahead (if the implementation of search_course() is later in the file, or in another file) so will not know what search_course() is without that declaration.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by qny View Post
    I think you forgot a return x; in the definition of myFunction().
    Thank you very much :-D

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I wrote my code as below:
    It's no longer there - Why did you delete it?
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function Prototype
    By popnfresh12321 in forum C Programming
    Replies: 1
    Last Post: 05-06-2012, 09:36 PM
  2. function prototype
    By eurikau in forum C Programming
    Replies: 1
    Last Post: 07-31-2007, 01:19 AM
  3. function prototype
    By shuo in forum C++ Programming
    Replies: 9
    Last Post: 04-19-2007, 09:25 PM
  4. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  5. Function ... should have prototype ?
    By c-- in forum C Programming
    Replies: 4
    Last Post: 12-27-2001, 10:49 PM