Jump to content

A little stupid question!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
8 replies to this topic

#1
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
I started learning about functions in C++. It can sound stupid but I am learning from two different books at the same time.

One book gives this:

#include <iostream>
using namespace std;

double sum(float x, float y)
{
    return (x+y);
}

int main()
{
    double a,b,S;
    cout << "A = ";
    cin >> a;
    cout << "B = ";
    cin >> b;

    S = sum(a,b);

    cout << "S = " << S << endl;
    return 0;
}

And the other one this:

#include <iostream>

using namespace std;

double sum(float x, float y);

int main()
{
    double a,b,S;
    cout << "A = ";
    cin >> a;
    cout << "B = ";
    cin >> b;

    S = sum(a,b);

    cout << "S = " << S << endl;
}
double sum(float x, float y)
{
    return (x+y);
}

Is there any difference, should I worry about it?


PS: This is a tiny example I made by myself so you all can notice the difference without going through too much code.

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#2
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
Before a function is called, the compiler needs to know the prototype to see that it is used correctly. A function definition is also a prototype. So both examples provide a prototype prior to the function call. Given that, the location of the function definition can be the programmer's choice.

#3
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
Which one would u recommend me to use and why?

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#4
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
If it is a larger project, I group related functions together in a separate file, call it "project.cpp". The public functions of this module I would then prototype in "project.h" and include where necessary.

Why? I find it easier.

#5
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
Thanx buddy :D

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#6
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
Another reason PHP is better! You do not need to declare a function BEFORE you call it, you can declare it after and it would still work!

<?PHP
marco();
function polo() {
	echo "Polo<br />";
	marco();
}
function marco() {
	echo "Marco<br />";
	polo();
}


#7
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
But PHP isn't always better.

Really though, it all comes down to personal choice. I always thought it was weird to have a function prototype and then the function after main. So I always put my functions before main.

#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts
I put functions before main for "toy" programs -- scratchpad work. It makes it easier to change both the prototype and signature at the function definition.

As projects get larger and more complicated, though, I organize different parts of code in separate modules and headers. Here, the extra little pain (if I choose not to use my editor's refactoring abilities) is made up for by the organization already in place.

But then again, most of the kinks get worked out in the "scratchpad" phase. It's a bit of give and take, which is why I mentioned that it becomes programmer's preference.

When all is said and done, however, "nice" code will generally have its functions prototyped in a header and the implementations will lie elsewhere. Consider the standard libraries or 3rd party libraries.

I think I'm starting to ramble, so I'll just stop writing. :p

[edit]And really, "language X is better" is mostly off-topic in a language-specific forum unless a particular task is being discussed (i.e., string handling, etc.).

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I do the same as dcs for most of my code. The only time it can become an issue is if function a() calls b(), and b() calls a() (think wacked recursion). Then ONE of them will have to have a function prototype before the other's definition.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog