Thread: Help with creating functions

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    25

    Help with creating functions

    Code:
    #include <stdio.h>
    
    
    main()
    {
            int first,second;
    
            printf("Enter a number: ");
            scanf("%d", &first);
            printf("\nEnter another number: ");
            scanf("%d", &second);
    
            find_max(first,second);
    }
    
    find_max(x,y)
    int x,y;
    {
            int max;
    
            if (x>=y)
                    max = x;
            else
                    max = y;
            printf("\nThe max is %d", max);
    }
    I dont know if Im doing this the wrong way, or if my compiler likes to play games with me. Im gonna lean over to the side that says i dont know what the hell im doing anymore, so please help.

    When i go to compile this, it says called to undefined function (find_max).

    I keep checking back and forth with the book im learning from, and it shows it should be correct, but the compiler thinks diffrently.

    heeeeeeeelp meeeeeeeeee.

    THX

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    25
    And also, while on the subject of functions, what does the returning value of a function actually do?

    Basically, what is the use of having a function return a value, and how is it used.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    First you need to prototype your function. That means you have to create a declaration so that the compiler knows about the function. Now you are using find_max() in main() before the compiler knows about find_max(). Prototyping goes like this:

    Code:
    void find_max (int a, int b);
    Your function needs to have a type. In your situation it would be like this:

    Code:
    void find_max (int a, int b)
    {
      ...
    }
    Also note that I've used the new style C here. The style you are using is quite old, or are you using an old compiler?

    A function can return a value. The purpose of letting a function return a value is that in the calling function the return value can be used. Like this:

    Code:
    #include <stdio.h>
    
    int find_max (int a, int b);
    
    int main ()
    {
      int max, a = 1, b = 2;
      max = find_max (a, b);
      printf ("Max is %d\n", max);
      return 0;
    }
    
    int find_max (int a, int b)
    {
      if (a > b)
        return a;
      else
        return b;
    }
    Here the function find_max() returns a value of type int and it is stored in a variable of type int.

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    25
    Damn, answered all my questions.

    Thanks a lot, book neglected to mention i need to declare functions when using main() it...

    And yes, this would be old style C, because the book is old... Copyright 1998.

    Guess buying a newer book in C would help.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    >And yes, this would be old style C, because the book is old... Copyright 1998.

    No way 1998 and still use old-fashion function declaration? what is the name of the book?

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    Believe it or not, 1998 is old for a C book. There was a new standard, called C99, which makes more strict many things about C.

  7. #7
    Registered User
    Join Date
    May 2003
    Posts
    25
    lol 1998..

    i meant 1988.

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ohh now its perfectly clear...
    and ygfperson, ok it's old but not like this

  9. #9
    Registered User
    Join Date
    Jun 2003
    Posts
    8
    You can also do the following to save time~\\

    #include <stdio.h>

    int find_max (int a, int b);

    int main ()
    {
    int max, a = 1, b = 2;
    \* I am nullifyig this statement as we do not need it 'max =find_max (a, b);'*/
    printf ("Max is %d\n",find_max (a, b));
    }

    int find_max (int a, int b)
    {
    if (a > b)
    return a;
    else
    return b;
    }

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. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 07:05 PM
  3. Creating push & pop functions... decrementing/tracking?
    By Sparrowhawk in forum C Programming
    Replies: 22
    Last Post: 12-14-2008, 09:10 PM
  4. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 12:50 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM