1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Calling function in C++

Discussion in 'C++' started by ballurohit, Feb 24, 2012.

  1. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    I do not understand how the Function in C++ can be called.

    Is it right to call the function as under:

    Void ReadName()
    {
    char *name;
    cout<<"Enter your name: ";
    cin>>name;
    cout << "\nYour name is "<<name;

    void main()

    {
    ReadName();
    return 0;
    }
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    you haven't allocated any space to store "name". right now, *name is a pointer that points to nothing.

    Code:
    void ReadName() {
    
       char *name = new char[20];
       ....
    }
    one other thing, cin >> name will break on white space, so if you intend to ask for a string including spaces, then use cin.getline(name, space allocated to name);

    HTH
     
  3. johnBMitchell

    johnBMitchell New Member

    Joined:
    Feb 17, 2011
    Messages:
    38
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    CA, USA
    Home Page:
    You can easily call any function without using pointer. You should define function as "int function_name(int a, int b)" and then put data types and variable in this function. After this, you can call this function in main() as “variable_name = funation_name(value1, value2)â€.
     
  4. ballurohit

    ballurohit New Member

    Joined:
    Nov 18, 2011
    Messages:
    43
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    retired
    Location:
    Gujarat
    QUOTE Thanking you very much.UNQUOTE:pleased:
     

Share This Page