473,794 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
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 2532
sh*******@yahoo .com wrote in news:1121922738 .228571.93040
@z14g2000cwz.go oglegroups.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<typena me _Tp>
inline void
swap(_Tp& __a, _Tp& __b)
{
// concept requirements
__glibcpp_funct ion_requires(_S GIAssignableCon cept<_Tp>)

_Tp __tmp = __a;
__a = __b;
__b = __tmp;
}
Jul 23 '05 #2
sh*******@yahoo .com wrote in news:1121922738 .228571.93040
@z14g2000cwz.go oglegroups.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<iostre am>
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<iostre am>
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<iostre am>
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************ **********@g49g 2000cwa.googleg roups.com>,
upashu2 <up*****@rediff mail.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
3860
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 writing something up on functions, and I don't like writing about things I am not sure about. Ok, then, here we go: I initially thought that one would only really need to use a function
6
7995
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 compiler can find the definition of the function prior to encountering the use of the function it will generate the prototype itself. I don't currently use this feature, I explicitly create declarations for all functions in a header file. However, I...
7
2380
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 main in either of the above mentioned ways my compiler does not give any warning. How can it accept two different prototypes ?
6
5013
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: Prototypes are only good for headers to be included in other files. For functions which call each other inside one file, such as A calls B which calls C and D, just define the functions in order D, C, B, A, and you'll never need prototypes.
9
1664
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 meaning or does it mean that func2 is a function that takes any number of arguments and returns an integer? Thanks for any clarification.
1
5561
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
7606
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
3468
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 prototypes. This kind of bugged me, because from other people's code that I've seen in the past, almost all of them use function prototypes. The following also bugged me. Let's say you have a file called main.c with only the main function, and...
29
8088
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 difference between a function prototype and a function declaration? If you get this right, you must have done fair amount of research on C
16
3289
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
10433
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.