Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 3.3: The Return Statement

Adapted from: “Object-Oriented Programming Using C++” by Ira Pohl (Addison- Wesley)

Program that demonstrates the RETURN statement in C++

// Find the minimum of two ints.
#include <iostream>

int min(int x, int y)
{
    if (x < y)
        return x;
    else
        return y;
}

int main()
{
    int j, k, m;

    std::cout << "Input two integers: ";
    std::cin >> j >> k;
    m = min(j, k);
    std::cout << '\n' << m << " is the minimum of " << j
              << " and " << k << std::endl;
}

Compile and Execute the Program

The above program is compiled and run using Gnu Compiler Collection (g++):

import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Cpp_Code/Chapter_3_3_The_Return_Statement"
ch_dir_stat = os.chdir(code_dir)
build_command = os.system("g++ min_int.cpp -w -o min_int")
exec_status = os.system("echo \"10 20\" | ./min_int")
Input two integers: 
10 is the minimum of 10 and 20
exec_status = os.system("echo \"89 27\" | ./min_int")
Input two integers: 
27 is the minimum of 89 and 27