Function prototypes in C++

S

shan_rish

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
 
A

Andre Kostur

(e-mail address removed) wrote in @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.)
 
E

E. Robert Tisdale

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
 
V

Vane

(e-mail address removed) wrote in @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.
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;
}
 
I

Ian

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
 
V

Vane

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.
 
M

MarcusRescue

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.
-----------
 
R

Richard Herring

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top