Just for fun, and because Goto Maki’s name makes a great programming pun, here’s a function in C/C++ that uses Goto’s name to actually do something (it computes the factorial of a number; the goto maki statement makes the program loop over the individual multiplications until the final product is computed):

int factorial(int n)
{
    int p = 1;
    maki:
    if (n == 0)
    {
        return p;
    }
    else
    {
        p *= n;
        n--;
        goto maki;
    }
}

You can put this in, say, a C++ program like the following:

gotomaki.cc

#include ;
using namespace std;

int factorial(int);

int main()
{
int n;
cout << "Enter a nonnegative integer to factorialize: "; cin >> n;
cout << "The factorial of " << n << " is " << factorial(n) << ".\n" << endl; return 0; } int factorial(int n) { int p = 1; maki: if (n == 0) { return p; } else { p *= n; n--; goto maki; } } [/sourcecode] and then you too (yes, you!) can factorialize away with Gocchin:

% g++ gotomaki.cc -o gotomaki
% ./gotomaki            
Enter a nonnegative integer to factorialize: 1
The factorial of 1 is 1.

% ./gotomaki
Enter a nonnegative integer to factorialize: 2
The factorial of 2 is 2.

% ./gotomaki
Enter a nonnegative integer to factorialize: 3
The factorial of 3 is 6.

% ./gotomaki
Enter a nonnegative integer to factorialize: 4
The factorial of 4 is 24.

% ./gotomaki
Enter a nonnegative integer to factorialize: 5
The factorial of 5 is 120.

% ./gotomaki
Enter a nonnegative integer to factorialize: 6
The factorial of 6 is 720.

% ./gotomaki
Enter a nonnegative integer to factorialize: 0
The factorial of 0 is 1.

Fun, ne?

Advertisements