Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Function prototypes in C++

Reply
Thread Tools

Function prototypes in C++

 
 
shan_rish@yahoo.com
Guest
Posts: n/a
 
      07-21-2005
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

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      07-21-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.)
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      07-21-2005
(E-Mail Removed) 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
 
Reply With Quote
 
Vane
Guest
Posts: n/a
 
      07-21-2005
(E-Mail Removed) 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;
}
 
Reply With Quote
 
upashu2
Guest
Posts: n/a
 
      07-21-2005
Most compiler allow this to keep comaptibllity with old C style
program.

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      07-21-2005
(E-Mail Removed) 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
>

 
Reply With Quote
 
Vane
Guest
Posts: n/a
 
      07-21-2005

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.
 
Reply With Quote
 
MarcusRescue
Guest
Posts: n/a
 
      07-21-2005
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.
-----------

 
Reply With Quote
 
Richard Herring
Guest
Posts: n/a
 
      07-21-2005
In message <(E-Mail Removed) .com>,
upashu2 <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
function prototypes and function addresses Syren Baran C Programming 6 01-09-2008 06:34 PM
Re: Can function prototypes appear anywhere? Lawrence Kirby C Programming 2 01-24-2005 04:56 PM
Re: Can function prototypes appear anywhere? Chris Dollin C Programming 3 01-19-2005 06:01 PM
order of const in function prototypes Denis Remezov C++ 7 06-30-2004 11:41 AM
New to Python; what about #include, extern and function prototypes Bo Jacobsen Python 6 03-07-2004 02:37 PM



Advertisments