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.2: Function Definitions

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

Program that demonstrates function definitions in C++

// Repeated bell ringing
#include <iostream>

const char BELL = '\a';

void ring(int k)
{
    int i;

    for (i=0; i<k; ++i) {
        std::cout << BELL;
        std::cout << "Ring!" << std::endl;
    }
}

int main ()
{
    int n;
    std::cout << "\nInput a small positive integer: " << std::endl;
    std::cin >> n;
    ring(n);
}

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_2_Function_Definition"
ch_dir_stat = os.chdir(code_dir)
build_command = os.system("g++ bellmltl.cpp -w -o bellmltl")
exec_status = os.system("echo \"10\" | ./bellmltl")

Input a small positive integer: 
Ring!
Ring!
Ring!
Ring!
Ring!
Ring!
Ring!
Ring!
Ring!
Ring!