Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ Can you explain return type functions?

I'm having trouble understanding how do return type functions work. If possible, describe how they work and provide an example.

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Are you asking about either the return type on functions or functions that have a return type, instead of being void?

    In C and C++ functions have the option of returning a value after their execution has completed. The return type tells the compiler what type of data will be returned just like when you're declaring a variable. Here are some examples.

    #include <iostream>

    using namespace std;

    // just print out what we're doing

    void print_div( int a, int b) {

    cout << a << " / " << b << endl;

    }

    // return the result of integer division

    int div( int a, int b ) {

    int c;

    c=a/b;

    return c;

    }

    // a shorter version

    int optimized_div( int a, int b ) {

    return a/b;

    }

    // use double-precision floating point math to achieve a more precise answer

    double float_div( int a, int b ) {

    return (double)a/(double)b

    }

  • oops
    Lv 6
    1 decade ago

    I can't do better job explaining it than this:

    http://www.cplusplus.com/doc/tutorial/functions/

Still have questions? Get your answers by asking now.