Functions Declaration,DefinitionShare your thoughts
15 Replies - 1044 Views - Last Post: 06 January 2009 - 08:15 AM
#1
Functions Declaration,Definition
Posted 05 January 2009 - 12:27 PM
Now,Is there a better way to do this?
Replies To: Functions Declaration,Definition
#2
Re: Functions Declaration,Definition
Posted 05 January 2009 - 01:13 PM
Sure, having a huge 2,000+ line program is fun to stare at, because you can look at it and go, "Jeez! I am a good programmer, I can understand all this 'complex' code! Horray!" but soon after you realize, that's not the objective.
The objective is to create as small and effecient code (and outcome *) as possible. You want to develop that code as fast as possible without creating lots of errors, which is why OOP (object-oriented programming) is in existance today.
I'm one of those clean and easy-to-read freaks too. I hate unformatted code.
As far as "filled with LOTS of functions" goes, try thinking of it like this:
Where do you really need a function?
- Is it called multiple times?
- Is it neccessary to put it into a function?
- Do you need to (or will you need to *) modify it alot
If all three are answered: Yes, yes - <GOOD reason here!>, and yes, then put it in a function.
Welcome, hope this helps (if it did, you know where to click).
#3
Re: Functions Declaration,Definition
Posted 05 January 2009 - 01:41 PM
EDIT:I use functions for easier debugging,For example the main file mostly consist of functions so i dont need to dig through the main file.
Still,maybe this is why i get confused when the project gets bigger
This post has been edited by Ion: 05 January 2009 - 01:48 PM
#4
Re: Functions Declaration,Definition
Posted 05 January 2009 - 01:50 PM
You never put a return line, made it hard to read.
Is there a better way? Um, depends on how you wrote it.
You should cluster meaningful "routines" into a single function, like.
Checking for unit damage/life/movement in one.
#5
Re: Functions Declaration,Definition
Posted 05 January 2009 - 01:53 PM
So there doesnt exist any other way to make functions available from other cpp files than declaring in a header file.
If this clarify my question,lol
#6
Re: Functions Declaration,Definition
Posted 05 January 2009 - 01:55 PM
#include is a preprocessor directive, it basically copies & pastes text from a file.
You typically use ".h" files because how the IDE views them. You can use *.cpp files to include other functions as well.
You can also link to functions within library files and dll files.
#7
Re: Functions Declaration,Definition
Posted 05 January 2009 - 02:02 PM
Lets say this is the main.cpp:
#include "mah.h"
int main()
{
mahfunction();
}
Now the mah.h file:
#include <iostream> using namespace std; void mahfunction();
Now the funcs.cpp file:
#include "mah.h"
void mahfunction()
{
cout << "The function!";
cin.get();
}
Can this be done in another way?
EDIT:Whops forgot the guards.Anyway,i know that they are to be in mah.h so dont pay attention
This post has been edited by Ion: 05 January 2009 - 02:06 PM
#8
Re: Functions Declaration,Definition
Posted 05 January 2009 - 02:10 PM
Also, I did understand your question, you mis understand the answer!
- Main.cpp
MAIN.CPP[/b]"]
#include <iostream>
#include "Mah.h"
int main() {
MahFunction();
return 0;
}
- Mah.h
MAH.H[/b]"]#ifndef MAH_H
#define MAH_H
void MahFunction() { printf("Hello MahFunction!"); }
#endif
There's your clarified answer.
EDIT: I hope that answers your question for once and all!
If I actually helped, remember to click thanks.
Sorry.
This post has been edited by Hyper: 05 January 2009 - 02:12 PM
#9
Re: Functions Declaration,Definition
Posted 05 January 2009 - 02:12 PM
extern varName; //etc...
#10
Re: Functions Declaration,Definition
Posted 05 January 2009 - 03:18 PM
KYA,is it extern FUNCNAME; or ? :S
#11
Re: Functions Declaration,Definition
Posted 05 January 2009 - 03:19 PM
quick edit: its extern type function or var name;
This post has been edited by KYA: 05 January 2009 - 03:25 PM
#12
Re: Functions Declaration,Definition
Posted 05 January 2009 - 03:24 PM
Main.cpp
#include <iostream>
using namespace std;
extern void functionName();
int main()
{
functionName();
system ("pause");
return 0;
}
External.h
#ifndef EXTERNAL_H #define EXTERNAL_H void functionName(); #endif
External.cpp
#include "External.h"
#include <iostream>
using namespace std;
void functionName()
{
cout << "I'm an externally defined function!\n";
}
The linker looks for functionName in External.cpp obj file so it can use it in Main. Putting the declaration in a header file was to illustrate common practice.
Hope this helps
#13
Re: Functions Declaration,Definition
Posted 05 January 2009 - 03:30 PM
#14
Re: Functions Declaration,Definition
Posted 05 January 2009 - 03:59 PM
if you really must put using namespace declarations anywhere, at least put them in your .cpp file where you'll not be doing quite so much damage.
#15
Re: Functions Declaration,Definition
Posted 05 January 2009 - 04:04 PM
|
|

New Topic/Question
Reply



MultiQuote




|