What's the difference??

This is a discussion on What's the difference?? within the C++ Programming forums, part of the General Programming Boards category; I'm fairly new to C++ and I've been reading this book, where I got to the new Chapter- FUNCTIONS..I wanna ...

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    What's the difference??

    I'm fairly new to C++ and I've been reading this book, where I got to the new Chapter- FUNCTIONS..I wanna know the difference between ....

    #include<iostream.h>
    int findMax(int, int);

    int main()
    {
    int firstnum, secnum, max;

    cout<<"\n Enter a Number: ";
    cin>>firstnum;
    cout<<"Great! Please Enter a second number: ";
    cin>>secnum;

    max=findMax(firstnum, secnum);

    cout<<"\n The Maximum of the two numbers is "<<max<<endl;

    return 0;
    }

    int findMax(int x, int y)
    {
    int maxnum;

    if(x >= y)
    maxnum= x;
    else
    maxnum=y;

    return maxnum;
    }


    ***************************and This.......

    #include<iostream.h>
    main()
    {
    int firstnum, secnum;

    cout<<"\n Please enter first number: ";
    cin>>firstnum;
    cout<<"\n Please enter second number : ";
    cin>>secnum;

    if(firstnum > secnum)
    cout<<firstnum<<" is larger ";
    else
    cout<<secnum<<" is larger ";

    return 0;
    }

    Someone please explain in plain english if possible..lol
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    The first example breaks the program into two functions. It does the same thing as the second function. Both find the larger of two integers.
    The difference is that if you want to do this operation again, you can just call the function again in the first example. In the second example, you have to write all the code again.
    The most basic thing that functions are used for is to break programs into modular segments that can be reused and called by other parts of the program.
    It also helps in debugging, and in making code easier to write and maintain.

  3. #3
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    Thanks for the quick reply...i understand now!
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Review required for program of date difference
    By chottachatri in forum C Programming
    Replies: 6
    Last Post: 10-31-2008, 11:46 AM
  2. Difference Equations / Recurrence Relations
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-05-2007, 10:26 AM
  3. What's the difference between var++ and ++var
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 05-31-2007, 02:27 AM
  4. how to get difference of digits
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 12-20-2001, 07:32 PM
  5. What is the Difference between ANSI C and non-ANSI C ?
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-24-2001, 05:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21