473,322 Members | 1,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Function prototypes in 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.

#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
8 2516
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
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
Most compiler allow this to keep comaptibllity with old C style
program.

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

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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Rob Somers | last post by:
Hey people, I read a good thread on here regarding the reason why we use function prototypes, and it answered most of my questions, but I wanted to double check on a couple of things, as I am...
6
by: Daniel Nichols | last post by:
I've noticed that in a C module (.c, .h file combination) that if you create a function's definition before it is used in other functions than a declaration is not necessary. I believe if the...
7
by: junky_fellow | last post by:
Can a function have two different prototypes ? If not , then how can main() have two different prototypes ? int main(void) and int main argc(int argc, char *argv) I mean to say, if I declare...
6
by: Robbie Hatley | last post by:
I'm maintaining a software project with 134 C++ files, some of them huge (as much as 10,000 lines each), and very few prototypes. The author's attitude towards prototypes was like this: ...
9
by: wizwx | last post by:
what does the following mean? int func2(); According to C++, it surely means func2 is a function that takes no argument and returns an integer. But what about in C? Does it have the same...
1
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
16
by: arne | last post by:
Hi all, imagine I call a function, but omit one of the parameters, like: foo.c: void foo( int a, int b ) { /* do something with a and b */ return; }
73
by: Steph Barklay | last post by:
Hi, I'm currently taking a data structures course in C, and my teacher said that function prototypes are not allowed in any of our code. He also said that no professional programmers use function...
29
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
16
by: Xiaoxiao | last post by:
Hi, I got a C library, is there a way to view the public function names in this library so that I can use in my C program? Thanks.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.