User-Defined Functions in C++

There are two types of user defined functions in C++, value-returning functions that have a data type, and void functions that do not return a value.

To use a library function, we need to know the name of the header file that contains the functions’ specification. We must include this header file in our program using the include statement.

We can use the value returned by a value-returning function in one of three ways, by saving the value for further computation, using the value immediately in a calculation, or printing the value. This means that value-returning functions are typically used in assignment statements, output statements, and as a parameter itself in another function call.

#include <iostream>
#include <cmath>

using namespace std;


int returnInt();

int main(void){
    
    int x = 7;
    int y = pow(x, 3);

    cout << "x = " << x << endl;
    cout << "x ^ 3 = " << y << endl;
    cout << "returnInt = " << returnInt() << endl;


    return 0;
}


int returnInt(){
    return 47;
}

A function definition contains a list of formal parameters, which are the variables declared in  function heading. As we have seen above, a function’s formal parameter list can be empty. The return type is also declared. The statements enclosed between curly braces make up the body of the function.

Once a value-returning function has processed the arguments, the function returns the value via the return statement. The return statement can return either  a variable, a literal value, or even an expression.

#include <iostream>

using namespace std;

int returnLiteral();
double returnVariable(int x);
int returnExpression(int x, int y);

int main(void){

    cout << "returnLiteral() = " << returnLiteral() << endl;
    cout << "returnVariable(73) = " << returnVariable(73) << endl;
    cout << "returnExpression(2600, 5200) = " << returnExpression(2600, 5200) << endl;

    return 0;

}


int returnLiteral(){
    return 42;
}

double returnVariable(int x){
    double y = x + 1.491625;
    return y;
}

int returnExpression(int x, int y){
    return x + y;
}

Note that in C++, return is  reserved word.

As in the programs above, user-defined functions are typically placed after the main() function. However, you must declare a function before you can use it. To get around this rule, we place function prototypes before any function definition. A function prototype is simply the function header, without the body of the function. Technically, we do not need to include the variable names in the function prototype, but why not just leave them in? It makes the code more readable, for one.

#include <iostream>
#include <cmath>

using namespace std;

int larger(double x, double y);

int main(void){

    double x, y;

    cout << "Enter value x: ";
    cin >> x;

    cout << "Enter value y: ";
    cin >> y;

    switch(larger(x, y)){
        case -1:
            cout << "x (" << x << ") is larger." << endl;
            break;
        case 1:
            cout << "y (" << y << ") is larger." << endl;
            break;
        case 0:
            cout << "x (" << x << ") and y (" << y << ") are equal." << endl;
            break;
    }

    return 0;

}


int larger(double x, double y){
    double epsilon = 0.00000001;
    
    if(fabs(x - y) < epsilon){
        return 0; //equal
    }

    if(x > y){
        return -1; //y is not larger
    }

    return 1; //y is larger
}

Note that once a function has been defined, it can be used multiple times.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s