help with functions.

This is a discussion on help with functions. within the C++ Programming forums, part of the General Programming Boards category; Code: #include<iostream> #include<conio.h> using namespace std ; int addy (int a,int b) { int r; cout<<"enter value for a:\t"; cin>>a; ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    2

    help with functions.

    Code:
    #include<iostream>
    #include<conio.h>
    using namespace std ;
    int addy (int a,int b)
    {
        int r;
        cout<<"enter value for a:\t";
        cin>>a;
        cout<<"enter value for b:\t";
        cin>>b;
        r=a+b;
        return (r);
    }
    int main ()
    {
       i want to cal function here... 
        
        
    getch();
        }
    in this code i want to get input from user in function .. that whenever function is called it gets two values from user and do addition on them and return them can anyone sort it out?

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    No need for parameters if you are doing the input within the function, and a return value is not needed since the address of the return value is temporary anyways.

    Code:
    int addy()
    {
     int a,b;
     std::cout << "Enter value for a:";
     std::cin >> a;
     std::cout << "Enter value for b:";
     std::cin >> b;
     return (a+b);
    }
    
    int main()
    {
      addy();
      getch();
      return 0;
    }

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    Quote Originally Posted by Raigne View Post
    No need for parameters if you are doing the input within the function, and a return value is not needed since the address of the return value is temporary anyways.
    But

    return (a+b);

    is still a return value, because the expression (a+b) results in a value. There are few expressions in C++ which don't result in values of some type; off-hand I can only think of function calls which return nothing. A return value is necessary because otherwise you would not get any of the information that addy(); computes for you.

    If you elect not to use return values, you have to use references or the like to change a pre-existing integer.

    That looks like this.

    Code:
    void addy (int &retval)
    {
       int a,b;
       std::cout << "Enter value for a:";
       std::cin >> a;
       std::cout << "Enter value for b:";
       std::cin >> b;
       retval = a + b;
    }
    
    //  called like
    int retval = 0;
    addy(retval);
    If a function returns something, you need to "use it or lose it" or "store it or ignore it". Example, if addy(); returns an int then:
    Code:
    retval = addy(); // storing it
    addy(); // ignoring it
    
    std::cout << addy(); // using it
    addy(); // losing it
    It's not really a complex choice, but if a function returns something you should probably use or check what the value is. It usually means something useful. And in this case, addy() is a sum, so you wouldn't call it if you didn't want a sum.
    Last edited by whiteflags; 04-22-2011 at 06:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  2. Calling functions from functions
    By droseman in forum C Programming
    Replies: 4
    Last Post: 01-21-2010, 05:33 AM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 10:50 AM

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