By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,120 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,120 IT Pros & Developers. It's quick & easy.

Function prototypes in C++

P: n/a
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 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
sh*******@yahoo.com 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;
}
Jul 23 '05 #2

P: n/a
sh*******@yahoo.com 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.)
Jul 23 '05 #3

P: n/a
Most compiler allow this to keep comaptibllity with old C style
program.

Jul 23 '05 #4

P: n/a
Ian
sh*******@yahoo.com 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

Jul 23 '05 #5

P: n/a
sh*******@yahoo.com 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
Jul 23 '05 #6

P: n/a

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.
Jul 23 '05 #7

P: n/a
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.
-----------

Jul 23 '05 #8

P: n/a
In message <11**********************@g49g2000cwa.googlegroups .com>,
upashu2 <up*****@rediffmail.com> 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
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.