Functions Declaration,Definition

Share your thoughts

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1159 Views - Last Post: 06 January 2009 - 08:15 AM Rate Topic: -----

#1 Ion  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-December 08

Functions Declaration,Definition

Post icon  Posted 05 January 2009 - 12:27 PM

Hey,Im one of those that want clean and easy read code.Though i dont want a big main file filled with LOTS of functions so i split it up in cpp files that use a global header file to find eachother.
Now,Is there a better way to do this?
Is This A Good Question/Topic? 0
  • +

Replies To: Functions Declaration,Definition

#2 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 01:13 PM

Ion, Very small, simple and yet concise code is exactly! What you want.
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). :P
Was This Post Helpful? 0
  • +
  • -

#3 Ion  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-December 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 01:41 PM

Well my question was really if doing this was possible in another way.
:P Sorry that i wasnt specific lol

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 :crazy:

This post has been edited by Ion: 05 January 2009 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#4 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 01:50 PM

Oh! I'm so sorry, I totally mis read what you'd said.
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.
Was This Post Helpful? 0
  • +
  • -

#5 Ion  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-December 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 01:53 PM

I see what you mean.
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
Was This Post Helpful? 0
  • +
  • -

#6 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 01:55 PM

You can write a full and complete C++ program using Notepad.
#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.
Was This Post Helpful? 0
  • +
  • -

#7 Ion  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-December 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 02:02 PM

Okay it looks like you read my question wrong again.I will make a code example

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

Was This Post Helpful? 0
  • +
  • -

#8 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 02:10 PM

Yes, it can be done another way.
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

Was This Post Helpful? 1
  • +
  • -

#9 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Functions Declaration,Definition

Posted 05 January 2009 - 02:12 PM

Also, say I have one function defined in one file and I need to use it in another:

extern varName; //etc...


Was This Post Helpful? 1
  • +
  • -

#10 Ion  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-December 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 03:18 PM

Thanks for your help,Hyper
KYA,is it extern FUNCNAME; or ? :S
Was This Post Helpful? 0
  • +
  • -

#11 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Functions Declaration,Definition

Posted 05 January 2009 - 03:19 PM

Let me work up a quick example.

quick edit: its extern type function or var name;

This post has been edited by KYA: 05 January 2009 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#12 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Functions Declaration,Definition

Posted 05 January 2009 - 03:24 PM

Ok here it is:

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 :)
Was This Post Helpful? 1
  • +
  • -

#13 Ion  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 78
  • Joined: 20-December 08

Re: Functions Declaration,Definition

Posted 05 January 2009 - 03:30 PM

Thanks,helped me
Was This Post Helpful? 0
  • +
  • -

#14 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: Functions Declaration,Definition

Posted 05 January 2009 - 03:59 PM

One minor, but important note to remember - putting something like using namespace std; inside a header file is a very bad idea, because you'll be polluting the global namespace with names from std, for anyone else who includes your header file regardless of whether they want them or not.

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.
Was This Post Helpful? 0
  • +
  • -

#15 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Functions Declaration,Definition

Posted 05 January 2009 - 04:04 PM

Not sure if that was just a general recommendation or if it was directed at me, but I didn't put that in the example header I showed.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2