Jump to content

basic function in c++

- - - - -

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

#1
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
Hi. A few questions came to my mind today on function. I am going to give you two sample codes.

1. Sample from Book How to Program C++ by Deitel & Deitel
#include <iostream>


int square ( int ); // function prototype


int main()

{

   for ( int x = 1; x <= 10; x++ )

      cout << square (x) << " ";


      cout << endl;

      return 0;

}


// function definition

int square ( int y )

{

   return y * y;

}



2. my own version based on #1
#include <iostream>

using namespace std;


int square ( int x )

{

   int r;

   r = x * x;

   return (r);

}


int main ()

{

   for ( int x = 1; x <= 10; x++ )

   {

       cout << square ( x ) << " " << endl;

   }

cin.get();

return 0;

}

I was confused in the first place (by the book), so I looked it up on the website.
Functions (I)

You can tell that my version (#2) is written in the style suggested by the tutorial.

My questions are:

1. For a simple program, which way do you prefer?
2. For a large program, or real world programming, which way do you prefer?
3. I understand how function work, but is not clear with #1 original code. The program begins with int main and when the compiler reaches square ( x ), which one does it return to? Is it the prototype first, or the definition second? I am not quite clear with those two lines.

Thank you very much.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
#include <iostream>


int square ( int ); // function prototype


int main()

{

   for ( int x = 1; x <= 10; x++ )

      cout << square (x) << " ";


      cout << endl;

      return 0;

}


// function definition

int square ( int y )

{

   return y * y;

}

A function prototype is a declaration, not a definition. It allows you to assert the existence of a function, but delay defining it until later. The advantage uf using function prototypes is they allow you to briefly declare your functions and then dive into main(). You have to declare or define a function before you can call it.

#include <iostream>

using namespace std;


int square ( int x )

{

   int r;

   r = x * x;

   return (r);

}


int main ()

{

   for ( int x = 1; x <= 10; x++ )

   {

       cout << square ( x ) << " " << endl;

   }

cin.get();

return 0;

}
Here, you dive straight into the definition, which qualifies as a declaration as well. The disadvantage here is that you have to wade through a lot of other code before you can find main().

jwxie518 said:

1. For a simple program, which way do you prefer?
2. For a large program, or real world programming, which way do you prefer?
3. I understand how function work, but is not clear with #1 original code. The program begins with int main and when the compiler reaches square ( x ), which one does it return to? Is it the prototype first, or the definition second? I am not quite clear with those two lines.

Thank you very much.
1. I prefer example 2, though I prefer the implementation of the function from the first example.
2. Example 2 or I'll start pulling utility functions into a separate file and put the declarations in a header file.
3. A declaration simple introduces a name into the program as a meaningful object. This makes it legal to call the function in main() when the function hasn't been defined yet. The definition, after main(), is what is actually called. You can read it roughly as follows:


Hey, head's up, there's going to be a function called "square" that accepts a parameter of type int and returns int.  Keep an eye out for it!  Love ya!


Here's function main().  It returns an int.  It uses a for loop to output the results of that square function I mentioned.  Yeah, I haven't gotten around to defining "square" yet, but I told to look out for it... it should be up ahead.


Hey, remember that function called "square"?  Here it is!  It just returns the number passed to it multiplied by itself.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jwxie518

jwxie518

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,180 posts
Thanks Panther. Nice and great explanation. I also like model explanation you made (#3)

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I'm glad you liked it :) It was fun to write.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog