Function prototypes in C++

Discussion in 'C++' started by shan_rish@yahoo.com, Jul 21, 2005.

  1. Guest

    I gather from this group's posts that there is no need of function
    prototypes if the function is "seen" before it is called. (i.e)the
    function should be defined before the main().
    But in the following code i defined the function after the main() and
    the code still works. Any thoughts. I am using Bloodshed C++ compiler.

    #include<iostream>
    using namespace std;

    int main()
    {
    int x=10, y=20;
    swap(x,y);
    cout<<"x = "<<x<<" y = "<<y;
    cin>>x;
    return 0;
    }

    void swap(int& i, int& j)
    {
    int tmp = i;
    i = j;
    j = tmp;
    }

    Cheers
    Shan
    , Jul 21, 2005
    #1
    1. Advertising

  2. Andre Kostur Guest

    wrote in news:1121922738.228571.93040
    @z14g2000cwz.googlegroups.com:

    > I gather from this group's posts that there is no need of function
    > prototypes if the function is "seen" before it is called. (i.e)the
    > function should be defined before the main().
    > But in the following code i defined the function after the main() and
    > the code still works. Any thoughts. I am using Bloodshed C++ compiler.
    >
    > #include<iostream>
    > using namespace std;
    >
    > int main()
    > {
    > int x=10, y=20;
    > swap(x,y);
    > cout<<"x = "<<x<<" y = "<<y;
    > cin>>x;
    > return 0;
    > }
    >
    > void swap(int& i, int& j)
    > {
    > int tmp = i;
    > i = j;
    > j = tmp;
    > }


    Bad choice of name for the function to test with... there exists a
    std::swap, and in your compiler, including iostream apparently brings it
    in. And also you have just experienced the main drawback of "using
    namespace std". Since you brought in all of the std namespace into the
    global namespace, the compiler had no problem in finding std::swap<int,
    int>() for you. It's not even ambiguous since it hasn't seen your swap
    (int&, int&) function yet. If instead you had done:

    using std::cout;
    using std::cin;

    the compiler would have told you that swap() doesn't exist. (Assuming
    your compiler respects namespaces properly. Not all do... IIRC older
    g++'s didn't.)
    Andre Kostur, Jul 21, 2005
    #2
    1. Advertising

  3. wrote:

    > I gather from this group's posts that there is no need of function
    > prototypes if the function is "seen" before it is called. (i.e the
    > function should be defined before the main()).
    > But in the following code, I defined the function after the main() and
    > the code still works. Any thoughts. I am using Bloodshed C++ compiler.


    > cat main.cc

    #include <iostream>


    int main(int argc, char* argv[]) {
    int x = 10, y = 20;
    swap(x, y);
    std::cout << "x = " << x << " y = " << y;
    std::cin >> x;
    return 0;
    }


    void swap(int& i, int& j) {
    int tmp = i;
    i = j;
    j = tmp;
    }

    > g++ -Wall -ansi -pedantic -o main main.cc

    main.cc: In function `int main(int, char**)':
    main.cc:5: error: `swap' undeclared (first use this function)
    main.cc:5: error: (Each undeclared identifier \
    is reported only once for each function it appears in.)
    main.cc: In function `void swap(int&, int&)':
    main.cc:11: error: `void swap(int&, int&)' \
    used prior to declaration
    E. Robert Tisdale, Jul 21, 2005
    #3
  4. Vane Guest

    wrote in news:1121922738.228571.93040
    @z14g2000cwz.googlegroups.com:

    > I gather from this group's posts that there is no need of function
    > prototypes if the function is "seen" before it is called. (i.e)the
    > function should be defined before the main().
    > But in the following code i defined the function after the main() and
    > the code still works. Any thoughts. I am using Bloodshed C++ compiler.

    On 21 ÆßÔÂ 2005, you wrote in comp.lang.c++:

    > I gather from this group's posts that there is no need of function
    > prototypes if the function is "seen" before it is called. (i.e)the
    > function should be defined before the main().
    > But in the following code i defined the function after the main() and
    > the code still works. Any thoughts. I am using Bloodshed C++ compiler.
    >


    //-----------------------------------------------------------------------
    void swap() has been defined in file "stl_algobase.h".


    /**
    * @brief Swaps two values.
    * @param a A thing of arbitrary type.
    * @param b Another thing of arbitrary type.
    * @return Nothing.
    *
    * This is the simple classic generic implementation. It will work on
    * any type which has a copy constructor and an assignment operator.
    */
    template<typename _Tp>
    inline void
    swap(_Tp& __a, _Tp& __b)
    {
    // concept requirements
    __glibcpp_function_requires(_SGIAssignableConcept<_Tp>)

    _Tp __tmp = __a;
    __a = __b;
    __b = __tmp;
    }
    Vane, Jul 21, 2005
    #4
  5. upashu2 Guest

    Most compiler allow this to keep comaptibllity with old C style
    program.
    upashu2, Jul 21, 2005
    #5
  6. Ian Guest

    wrote:
    > I gather from this group's posts that there is no need of function
    > prototypes if the function is "seen" before it is called. (i.e)the
    > function should be defined before the main().
    > But in the following code i defined the function after the main() and
    > the code still works. Any thoughts. I am using Bloodshed C++ compiler.
    >
    > #include<iostream>
    > using namespace std;

    ^^
    remove this and see what happens.

    Ian
    > int main()
    > {
    > int x=10, y=20;
    > swap(x,y);
    > cout<<"x = "<<x<<" y = "<<y;
    > cin>>x;
    > return 0;
    > }
    >
    > void swap(int& i, int& j)
    > {
    > int tmp = i;
    > i = j;
    > j = tmp;
    > }
    >
    > Cheers
    > Shan
    >
    Ian, Jul 21, 2005
    #6
  7. Vane Guest

    Because you are using Bloodshed C++ compiler, and the file
    "stl_algobase.h" is in the folder "\include\c++\3.3.1\bits\".

    U can delete
    > void swap(int& i, int& j)
    > {
    > int tmp = i;
    > i = j;
    > j = tmp;
    > }
    >

    and try again.
    Vane, Jul 21, 2005
    #7
  8. MarcusRescue Guest

    that is a translation of originl in Portuguese
    ----------------------------------------------------------

    This happened because you it used namespace STD mattering its symbols
    for the global space of names. As the function "swap" is defined in
    std, you simply it overloaded the function. It removes the line where
    you use namespace STD or create a funcão that is not defined in STD
    and tries to compile to see what it happens. :)

    #include<iostream>
    using namespace std;

    int main()
    {
    int x=10, y=20;
    test(x,y);
    cout<<"x = "<<x<<" y = "<<y;
    cin>>x;
    return 0;
    }


    void test(int& i, int& j){
    i = j + i;
    j = i - j;
    i = i - j;
    }

    It notices that "test" will occur an error therefore is not defined in
    SDT or possesss an prototypes.
    ----------------------------------------------------------------
    Isto aconteceu porque você utilizou o namespace STD importando os seus
    símbolos para o espaço de nomes global.
    Como a função "swap" é definida em std, você simplesmente
    sobrecarregou a função.
    Retire a linha em que você utiliza o namespace STD ou crie uma funcão
    que não esteja definida em STD e tente compilar para ver o que
    acontece. :)

    Note que ocorrerá um erro pois "test" não está definido em SDT ou
    possui um protótipo.
    -----------
    MarcusRescue, Jul 21, 2005
    #8
  9. In message <>,
    upashu2 <> writes
    >Most compiler allow this


    What is "this"?

    >to keep comaptibllity with old C style
    >program.
    >

    Name five.

    As other posters have explained, the combination of including a standard
    header and a using-directive for namespace std:: has exposed std::swap.
    It's nothing to do with compatibility with obsolete programs.

    --
    Richard Herring
    Richard Herring, Jul 21, 2005
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denis Remezov

    order of const in function prototypes

    Denis Remezov, Jun 30, 2004, in forum: C++
    Replies:
    7
    Views:
    422
    John Harrison
    Jun 30, 2004
  2. Bo Jacobsen
    Replies:
    6
    Views:
    497
    David M. Cook
    Mar 7, 2004
  3. Chris Dollin

    Re: Can function prototypes appear anywhere?

    Chris Dollin, Jan 19, 2005, in forum: C Programming
    Replies:
    3
    Views:
    345
    Michael Wojcik
    Jan 19, 2005
  4. Lawrence Kirby

    Re: Can function prototypes appear anywhere?

    Lawrence Kirby, Jan 20, 2005, in forum: C Programming
    Replies:
    2
    Views:
    326
    Lawrence Kirby
    Jan 24, 2005
  5. Syren Baran

    function prototypes and function addresses

    Syren Baran, Jan 9, 2008, in forum: C Programming
    Replies:
    6
    Views:
    300
Loading...

Share This Page