Functions.....

This is a discussion on Functions..... within the C++ Programming forums, part of the General Programming Boards category; Ok, here is an example used in the tutorial: #include <iostream.h> int mult(int x, int y); int main() { int ...

  1. #1
    Unregistered
    Guest

    Functions.....

    Ok, here is an example used in the tutorial: #include <iostream.h>

    int mult(int x, int y);

    int main()
    {
    int x, y;
    cout<<"Please input two numbers to be multiplied: ";
    cin>>x>>y;
    cout<<"The product of your two numbers is "<<mult(x, y);
    return 0;
    }
    int mult(int x, int y)
    {
    return x*y;
    }

    Now, here's what is much shorter and does the same thing: #include <iostream.h>

    int main()
    {
    int a;
    int b;
    cout<<"enter two numbers to be multiplied: ";
    cin>>a>>b;
    int c = a*b;
    cout<<"The product of your two numbers is: "<<c;
    return 0;

    }


    Soo....whats the use of what they did in the tutorial? Also, I don't get it anyways, someone care to explain?

  2. #2
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,738
    the function in the tutorial is pretty much useless...they just did it to show you an example.

    Functions can be incredibly helpful, and are used in any and all professional code.

    There are some things that you will need to use several times, and so you dont want to type it out several times. For example, lets say you wanted to sort a list of numbers (assuming you know basic arrays).

    In a bubble sort, the code is:

    int temp;
    for(int x = 0; x < listlength; x++)
    {
    for(int y = 0; y < x; y++)
    {
    if(x > y) { //swap the vars
    temp = x;
    x = y;
    y = temp;
    }
    }
    }

    Now, if you wanted to sort lots of list using the bubble sort, would you want to type it out 10000 times? No. So you use functions:

    void BubbleSort (int list[], int listlength) {
    int temp;
    for(int x = 0; x < listlength; x++)
    {
    for(int y = 0; y < x; y++)
    {
    if(x > y) { //swap the vars
    temp = x;
    x = y;
    y = temp;
    }
    }
    }
    }

    Then, you could just say:

    BubbleSort(myList, 10); //as an example, we will say the list is 10 elements long

    Hope this helps.
    My Website

    "Circular logic is good because it is."

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    22
    It is just trying to give you an example of a function... a function can be used over and over again if you need to, so you don't have to keep re-writing the code. On a simple program it is easier to use what your example was, but on larger programs, it is better to use the function.
    <^>( * ; * )<^>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 06:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 12:50 PM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 02:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 06:39 AM

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