Thread: Re: [Dev-C++] FunctionCalling()
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <jar...@ig...> - 2002-12-06 12:32:02
|
On 6 Dec, Dondolo Mpho wrote:
> Hi all,
>
> I am in the process of learning C++. I have been practising, writing my own
> functions and using main() only to call those function. I also use those
> functions to call up other function....
>
> //e.g.: function (1)
HERE you need a so-called forward declaration
just say
void FunctionTwo(); // <=== note the semi-colon here
// at a later point or within a different file
// you define the 'real' function with the SAME
// function header
>
> void FuctionOne(void)
> {
> int Answer
> cout<<"Are you ready...<press 1 for yes and 2 for no> ";
> cin>>Answer;
> if(Answer==1)
> {
> cout<<"\nI am glad that you are";
> }
>
> else
> {
> FuctionTwo();
> }
>
> }
>
> //Here I have my
> //FunctionTwo
>
> Now I find that the compiler flags an
> error msg that FunctionTwo has not been declared. it only works when I take
> FunctionTwo() and put it on top of
> FunctionOne()....i am I doing something
> wrong? The thing is I might decide halfway through my functions That i want
> function x to call function
> y. Now If i have to readjust my
> functions half way through, i will loose track of what I am doing.....Please
> help anybody
>
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
Aachen University
D 52056 Aachen, Germany
|
|
From: Daniel G. <sp...@ho...> - 2002-12-06 12:33:01
|
You have to use forward declaration.
At the moment, when the function is called the compiler says
"what's this? functionTwo()? I don't know about that"
Either write out your functions before your main function, like
#include <iostream.h>
int FunctionTwo( int Input )
{
Input = 365;
return Input;
}
int main()
{
int Input = 0;
FunctionTwo( Input );
cout << "End";
return 0;
}
Or type them all out in to a header file, then #include it.
// in the header file (Functions.h)
int FunctionTwo( int Input )
{
Input = 365;
return Input;
}
// in the main (.cpp) file
#include <iostream.h>
#include "Functions.h"
int main()
{
int Input = 0;
FunctionTwo( Input );
cout << "End";
return 0;
}
P.S. I know that's not the greatest example of coding, but I'm keeping it
simple as I got confused by examples when I started.
If you need help, post again!
>From: Dondolo Mpho <MDo...@OL...>
>To: 'dev-cpp-users-list' <dev...@li...>
>Subject: [Dev-C++] FunctionCalling()
>Date: Fri, 6 Dec 2002 13:05:20 +0200
>
>Hi all,
>
>I am in the process of learning C++. I have been practising, writing my own
>functions and using main() only to call those function. I also use those
>functions to call up other function....
>
>//e.g.: function (1)
>
>void FuctionOne(void)
>{
>int Answer
>cout<<"Are you ready...<press 1 for yes and 2 for no> ";
>cin>>Answer;
> if(Answer==1)
> {
>cout<<"\nI am glad that you are";
>}
>
>else
> {
> FuctionTwo();
> }
>
>}
>
>//Here I have my
>//FunctionTwo
>
> Now I find that the compiler flags an
>error msg that FunctionTwo has not been declared. it only works when I take
>FunctionTwo() and put it on top of
> FunctionOne()....i am I doing
>something
>wrong? The thing is I might decide halfway through my functions That i want
>function x to call function
> y. Now If i have to readjust my
>functions half way through, i will loose track of what I am
>doing.....Please
>help anybody
>
>
>Mpho Dondolo
>Support Analyst
>CSC Computer Sciences (South Africa) (Pty) Ltd
>Tel: +27 21 503030164
>cell: +27 84 3371468
>Fax: +27 21 5095619
>Email:MDo...@ol...
>Email: MDo...@cs...
>
>
_________________________________________________________________
The new MSN 8: smart spam protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
|
|
From: Brenden T. <br...@rc...> - 2002-12-06 17:46:30
|
Holy poop, batman! This is the worst idea ever!
Daniel Glenfield wrote:
> You have to use forward declaration.
Correct.
> Or type them all out in to a header file, then #include it.
No! Never! That is NOT a forward declaration!
Only put the prototype in the header file:
//Functions.h
void functionTwo( void );
Then include just the headers in the main program:
//main.cpp
#include <iostream>
#include "Functions.h"
void functionOne( void )
{
// ...
}
void functionTwo( void )
{
// ...
}
// end main.cpp
When you have functions like this, in one source file, it's common just
to put the prototype in the beginning of that source file and not use a
header file. However, you often need to call functions elsewhere, so
it's not bad to get in the habit of putting the prototypes in a header file.
My 2 pence!
Peace, B.
>
>
> // in the header file (Functions.h)
>
> int FunctionTwo( int Input )
> {
> Input = 365;
> return Input;
> }
>
>
> // in the main (.cpp) file
> #include <iostream.h>
> #include "Functions.h"
>
>
> int main()
> {
> int Input = 0;
> FunctionTwo( Input );
> cout << "End";
> return 0;
> }
>
>
> P.S. I know that's not the greatest example of coding, but I'm keeping
> it simple as I got confused by examples when I started.
>
> If you need help, post again!
>
>
>
>> From: Dondolo Mpho <MDo...@OL...>
>> To: 'dev-cpp-users-list' <dev...@li...>
>> Subject: [Dev-C++] FunctionCalling()
>> Date: Fri, 6 Dec 2002 13:05:20 +0200
>>
>> Hi all,
>>
>> I am in the process of learning C++. I have been practising, writing
>> my own
>> functions and using main() only to call those function. I also use those
>> functions to call up other function....
>>
>> //e.g.: function (1)
>>
>> void FuctionOne(void)
>> {
>> int Answer
>> cout<<"Are you ready...<press 1 for yes and 2 for no> ";
>> cin>>Answer;
>> if(Answer==1)
>> {
>> cout<<"\nI am glad that you are";
>> }
>>
>> else
>> {
>> FuctionTwo();
>> }
>>
>> }
>>
>> //Here I have my
>> //FunctionTwo
>>
>> Now I find that the compiler
>> flags an
>> error msg that FunctionTwo has not been declared. it only works when
>> I take
>> FunctionTwo() and put it on top of
>> FunctionOne()....i am I doing
>> something
>> wrong? The thing is I might decide halfway through my functions That
>> i want
>> function x to call function
>> y. Now If i have to readjust my
>> functions half way through, i will loose track of what I am
>> doing.....Please
>> help anybody
>>
>>
>> Mpho Dondolo
>> Support Analyst
>> CSC Computer Sciences (South Africa) (Pty) Ltd
>> Tel: +27 21 503030164
>> cell: +27 84 3371468
>> Fax: +27 21 5095619
>> Email:MDo...@ol...
>> Email: MDo...@cs...
>>
>>
>
>
> _________________________________________________________________
> The new MSN 8: smart spam protection and 2 months FREE*
> http://join.msn.com/?page=features/junkmail
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
>
|
|
From: Daniel G. <sp...@ho...> - 2002-12-06 18:01:06
|
whoops! didn't mean to say put the implementation in the header!
>From: Brenden Towey <br...@rc...>
>To: Daniel Glenfield <sp...@ho...>
>CC: MDo...@OL..., dev...@li...
>Subject: Re: [Dev-C++] FunctionCalling()
>Date: Fri, 06 Dec 2002 09:47:58 -0800
>
>Holy poop, batman! This is the worst idea ever!
>
>Daniel Glenfield wrote:
>
>>You have to use forward declaration.
>
>
>Correct.
>
>>Or type them all out in to a header file, then #include it.
>
>
>No! Never! That is NOT a forward declaration!
>
>Only put the prototype in the header file:
>//Functions.h
>void functionTwo( void );
>
>Then include just the headers in the main program:
>//main.cpp
>#include <iostream>
>#include "Functions.h"
>
>void functionOne( void )
>{
> // ...
>}
>
>void functionTwo( void )
>{
> // ...
>}
>
>// end main.cpp
>
>When you have functions like this, in one source file, it's common just to
>put the prototype in the beginning of that source file and not use a header
>file. However, you often need to call functions elsewhere, so it's not bad
>to get in the habit of putting the prototypes in a header file.
>
>My 2 pence!
>Peace, B.
>
>>
>>
>>// in the header file (Functions.h)
>>
>>int FunctionTwo( int Input )
>>{
>> Input = 365;
>> return Input;
>>}
>>
>>
>>// in the main (.cpp) file
>>#include <iostream.h>
>>#include "Functions.h"
>>
>>
>>int main()
>>{
>> int Input = 0;
>> FunctionTwo( Input );
>> cout << "End";
>> return 0;
>>}
>>
>>
>>P.S. I know that's not the greatest example of coding, but I'm keeping it
>>simple as I got confused by examples when I started.
>>
>>If you need help, post again!
>>
>>
>>
>>>From: Dondolo Mpho <MDo...@OL...>
>>>To: 'dev-cpp-users-list' <dev...@li...>
>>>Subject: [Dev-C++] FunctionCalling()
>>>Date: Fri, 6 Dec 2002 13:05:20 +0200
>>>
>>>Hi all,
>>>
>>>I am in the process of learning C++. I have been practising, writing my
>>>own
>>>functions and using main() only to call those function. I also use those
>>>functions to call up other function....
>>>
>>>//e.g.: function (1)
>>>
>>>void FuctionOne(void)
>>>{
>>>int Answer
>>>cout<<"Are you ready...<press 1 for yes and 2 for no> ";
>>>cin>>Answer;
>>> if(Answer==1)
>>> {
>>>cout<<"\nI am glad that you are";
>>>}
>>>
>>>else
>>> {
>>> FuctionTwo();
>>> }
>>>
>>>}
>>>
>>>//Here I have my
>>>//FunctionTwo
>>>
>>> Now I find that the compiler flags
>>>an
>>>error msg that FunctionTwo has not been declared. it only works when I
>>>take
>>>FunctionTwo() and put it on top of
>>> FunctionOne()....i am I doing
>>>something
>>>wrong? The thing is I might decide halfway through my functions That i
>>>want
>>>function x to call function
>>> y. Now If i have to readjust my
>>>functions half way through, i will loose track of what I am
>>>doing.....Please
>>>help anybody
>>>
>>>
>>>Mpho Dondolo
>>>Support Analyst
>>>CSC Computer Sciences (South Africa) (Pty) Ltd
>>>Tel: +27 21 503030164
>>>cell: +27 84 3371468
>>>Fax: +27 21 5095619
>>>Email:MDo...@ol...
>>>Email: MDo...@cs...
>>>
>>>
>>
>>
>>_________________________________________________________________
>>The new MSN 8: smart spam protection and 2 months FREE*
>>http://join.msn.com/?page=features/junkmail
>>
>>
>>
>>-------------------------------------------------------
>>This sf.net email is sponsored by:ThinkGeek
>>Welcome to geek heaven.
>>http://thinkgeek.com/sf
>>_______________________________________________
>>Dev-cpp-users mailing list
>>Dev...@li...
>>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>>
>>
_________________________________________________________________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail
|
|
From: Matthews <dem...@uw...> - 2002-12-07 17:17:03
|
Wouldn't this line=20
>f2("an integer");
cause an error because it's passing a quoted string rather than an integer
name or value? Remember, the function was declared like so:
>void f2 (int);
At 10:38 AM 06/12/2002 -0200, you wrote:
>You should use prototypes to define the functions you need
>in your main function.=20
>Define your prototypes before the main function or
>in another file (.h). The code for your functions=20
>can be after your "main".
>
>In your case, the prototypes would be:
>void FuctionOne(void);
>void FuctionTwo(void);
>
>Basically, they are the headers of functions with
>a ";" at the end.
>
>So you have, for instance:
>1) prototypes
>void f1 (void);
>void f2 (int);
>
>2) main function
>void main() {
>...
> f1();
> f2("an integer");
>...
>}
>
>3) implementation of the functions you define
>void f1 (void) {
> int myInt;=20
>...
> f2(myInt);
>...
>}
>
>void f2 (int anInt) {
>...
>}
>
>Read more about prototypes and functions in
>http://www.cplusplus.com/doc/tutorial/tut2-2.html
>
>Regards,
>Rafael=20
>
>C=F3pia Dondolo Mpho <MDo...@OL...>:
>
>> Hi all,
>>=20
>> I am in the process of learning C++. I have been practising, writing my
>> own
>> functions and using main() only to call those function. I also use
>> those
>> functions to call up other function....
>>=20
>> //e.g.: function (1)
>>=20
>> void FuctionOne(void)
>> {
>> int Answer=20
>> cout<<"Are you ready...<press 1 for yes and 2 for no> ";
>> cin>>Answer;
>> if(Answer=3D=3D1)
>> {
>> cout<<"\nI am glad that you are";
>> }
>>=20
>> else
>> {
>> FuctionTwo();
>> }
>>=20
>> }
>>=20
>> //Here I have my=20
>> //FunctionTwo
>>=20
>> Now I find that the compiler flags
>> an
>> error msg that FunctionTwo has not been declared. it only works when I
>> take
>> FunctionTwo() and put it on top of =20
>> FunctionOne()....i am I doing
>> something
>> wrong? The thing is I might decide halfway through my functions That i
>> want
>> function x to call function=20
>> y. Now If i have to readjust my
>> functions half way through, i will loose track of what I am
>> doing.....Please
>> help anybody =20
>>=20
>> =20
>> Mpho Dondolo
>> Support Analyst
>> CSC Computer Sciences (South Africa) (Pty) Ltd
>> Tel: +27 21 503030164
>> cell: +27 84 3371468
>> Fax: +27 21 5095619
>> Email:MDo...@ol...
>> Email: MDo...@cs...
>> =20
>>=20
>>=20
>
>
>-------------------------------------------------------
>This sf.net email is sponsored by:ThinkGeek
>Welcome to geek heaven.
>http://thinkgeek.com/sf
>_______________________________________________
>Dev-cpp-users mailing list
>Dev...@li...
>TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
>https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Rafael C. <cha...@un...> - 2002-12-09 12:25:45
|
Sorry! I didn't mean "an integer" as a string.
I meant it could be any integer argument.
void main () {
...
int i = 0;
f2(i);
...
}
OR
void main () {
...
int i;
i = i + 5;
f2(i);
...
}
OR
void main () {
...
f2(10);
...
}
Rafael
Matthews wrote:
> Wouldn't this line
>
>>f2("an integer");
>
> cause an error because it's passing a quoted string rather than an integer
> name or value? Remember, the function was declared like so:
>
>>void f2 (int);
>
>
|