Thread: Functions, Prototypes, Questions

  1. #1
    Registered User DeanDemon's Avatar
    Join Date
    Nov 2002
    Posts
    37

    Question Functions, Prototypes, Questions

    #include <iostream.h>
    int area (int wid, int len)
    {
    return wid * len;
    }
    main()
    {
    int wid = 5, len = 6;
    cout << area ( wid, len);
    }

    There's my code. While it still works perfectly fine, I dont quite understand the purpose of the area function in this particular code.

    And here are some other things in C++ that I'm not yet sure of what they mean:

    return 0;
    void function();
    end1

    If someone could help me out, I'd be a very happy person
    -Dean

  2. #2
    meagain
    Guest
    main()
    {
    int wid = 5, len = 6;
    cout << area ( wid, len);
    }

    when the code reaches the word 'area' it jumps into the area function, taking with it 2 variables - wid and len (actually they are copied into new variables). Now the area function returns the value of wid*len which is put into the output stream, and control returns to main()

    That probably not very clear, and it may not even be what you want.

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    well, basically, what functions are for are if the program needs to do something more than once or so, then the function is nice. it allows for the program to do the selected task, in this case, return the area. if main needs to call it, say 5 times, then the function allows you to only write the formula for area once. you need to send the area function parameters so it could do its job, hence the line.

    area(wid, len);

    return 0; - usually used when you have a function that returns anything but void, you need return in the function. i believe this line is for error checking, although im not sure (I'm probably wrong).

    void function();
    this is helpful when you have a function that all it needs to do is do something without giving an answer back to main.

    endl (by the way, probably a typo, but it is not end1)
    a (function) in iostream.h that lets you cout to another line, and i believe this also flushes the buffer.
    to start a new line \n could be used, but it doesn't flush the buffer.

    i hope this helps...correct me if i'm wrong though...

  4. #4
    Registered User jawwadalam's Avatar
    Join Date
    May 2002
    Posts
    131
    endl (by the way, probably a typo, but it is not end1)
    a (function) in iostream.h that lets you cout to another line, and i believe this also flushes the buffer.
    to start a new line \n could be used, but it doesn't flush the buffer.
    Do you mean that [B]cout<<'\n'<<flush; [\B]
    is equal to [B]cout<<endl;[\b] ????????
    Last edited by jawwadalam; 11-29-2002 at 08:03 PM.
    One day you will ask what more important to you..
    I will say my life..
    and You will leave me with even knowing
    that
    You are my Life (L)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  2. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  3. functions and prototypes (error)
    By Panzer_online in forum C Programming
    Replies: 6
    Last Post: 01-17-2004, 12:28 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Attaching functions to a class/struct
    By VirtualAce in forum Tech Board
    Replies: 2
    Last Post: 08-04-2003, 10:56 AM