Need some help with the exercise from the book

This is a discussion on Need some help with the exercise from the book within the C++ Programming forums, part of the General Programming Boards category; The thing is that when you call a function, the compiler must know about it. The compiler can know about ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    The thing is that when you call a function, the compiler must know about it.
    The compiler can know about a function in two ways.
    The first is by defining it (writing out the actual function) before the place where you call it (higher up in the code).
    The second is by putting a declaration. A prototype. If you do that, then the function may be defined anywhere in your code. It can be at the end of the source file, in another source file or even in a DLL or somewhere else.
    However, the declaration and definition must match. Just remember that one.

    Example of prototype which takes no arguments:
    Code:
    void myfunc();
    
    int main()
    {
        myfunc();
    }
    
    void myfunc()
    {
    }
    Example of function that takes one argument:
    Code:
    void myfunc(int n);
    
    int main()
    {
        myfunc(5);
    }
    
    void myfunc(int n)
    {
    }
    Example of incorrect prototypes:
    Code:
    void myfunc(float n); // Invalid: myfunc does not take a float
    void myfunc(); // Invalid, myfunc does not take no arguments
    void myfunc(float n, int n2); // Invalid, myfunc does not take two argument, a float and an int.
    void myfunc(int n); // Correct, myfunc takes one argument - an int,
    
    int main()
    {
        myfunc(5);
    }
    
    void myfunc(int n)
    {
    }
    See how all the prototypes which do not match the actual function we've written (the definition) are incorrect. The one that does match is correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Thanks so much and that's pretty much clear to me. But there is one thing I wanna verify is that the name of the argument_list does not necessarily have to match each other, does it? Let's take your code as an example:

    Code:
    void myfunc(float n); // Invalid: myfunc does not take a float
    void myfunc(); // Invalid, myfunc does not take no arguments
    void myfunc(float n, int n2); // Invalid, myfunc does not take two argument, a float and an int.
    void myfunc(int n); // Correct, myfunc takes one argument - an int,
    
    int main()
    {
        myfunc(5);
    }
    
    void myfunc(int n)
    {
    }
    Ok. for the prototype, you declare void myfunc(int n), and when it comes to calling it has to be myfunc(something here(anything)). But when it comes to the definition of the function, it is possible to name it void myfunc(int m), right? What they have to be the same are, the type and variable of the function as well as the type in the parameter and the argument_list, aren't they?

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Yes, the compiler really ignores the name of the parameters there, so they can be different.
    However, I would advise you that you do not leave them our not change their names, due to different reasons. Some IDEs, for example, take the information for intellisense from the prototypes, so it should be as accurate as possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kenryuakuma View Post
    Ok. for the prototype, you declare void myfunc(int n), and when it comes to calling it has to be myfunc(something here(anything)).
    It has to be myfunc(something here that is an int). You will get "lucky" if you pass in a float or a double, as those will get truncated down to an int, but if you pass a string or similar, nothing good is going to happen.

  5. #20
    Registered User
    Join Date
    Sep 2008
    Posts
    65
    Thanks so much tabstop and Elysia. Awesome! This is a great help to a beginner like me.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with K&R Book Exercise
    By Alejandrito in forum C Programming
    Replies: 5
    Last Post: 03-11-2008, 01:24 PM
  2. book exercise
    By luigi40 in forum C# Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 AM
  3. Exercise from my book.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 12-05-2002, 07:54 AM
  4. Books on C and C++
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-28-2002, 03:18 PM
  5. any recommended exercise book for C++?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2001, 03:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21