Home » Archimedes archive » Acorn User » AU 1995-10.adf » !Regulars » Regulars/C/c/levels

Regulars/C/c/levels

This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.

Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.

Tape/disk: Home » Archimedes archive » Acorn User » AU 1995-10.adf » !Regulars
Filename: Regulars/C/c/levels
Read OK:
File size: 02FC bytes
Load address: 0000
Exec address: 0000
File contents
/* C for yourself example program - October 1995
   by Steve Mumford */


#include <stdio.h>

int     main(void);
void    dive(int);

int
main(void)
{
        printf("This program uses recursion to 'dive' down a function\n");
        printf("to a certain level, then return safely.\n\n"); 
        dive(1);
}

void
dive(int x)
{
        if (x == 5) {
                printf("Reached turning point; now we work back through all\n");
                printf("   the functions we've just called.\n");
                return;
        }
        printf("Running forwards - level %d\n", x);
        dive(x+1);

        /* This is where control returns once the function below us
           has finished executing */

        printf("Running backwards - level %d\n", x);
}
00000000  2f 2a 20 43 20 66 6f 72  20 79 6f 75 72 73 65 6c  |/* C for yoursel|
00000010  66 20 65 78 61 6d 70 6c  65 20 70 72 6f 67 72 61  |f example progra|
00000020  6d 20 2d 20 4f 63 74 6f  62 65 72 20 31 39 39 35  |m - October 1995|
00000030  0a 20 20 20 62 79 20 53  74 65 76 65 20 4d 75 6d  |.   by Steve Mum|
00000040  66 6f 72 64 20 2a 2f 0a  0a 0a 23 69 6e 63 6c 75  |ford */...#inclu|
00000050  64 65 20 3c 73 74 64 69  6f 2e 68 3e 0a 0a 69 6e  |de <stdio.h>..in|
00000060  74 20 20 20 20 20 6d 61  69 6e 28 76 6f 69 64 29  |t     main(void)|
00000070  3b 0a 76 6f 69 64 20 20  20 20 64 69 76 65 28 69  |;.void    dive(i|
00000080  6e 74 29 3b 0a 0a 69 6e  74 0a 6d 61 69 6e 28 76  |nt);..int.main(v|
00000090  6f 69 64 29 0a 7b 0a 20  20 20 20 20 20 20 20 70  |oid).{.        p|
000000a0  72 69 6e 74 66 28 22 54  68 69 73 20 70 72 6f 67  |rintf("This prog|
000000b0  72 61 6d 20 75 73 65 73  20 72 65 63 75 72 73 69  |ram uses recursi|
000000c0  6f 6e 20 74 6f 20 27 64  69 76 65 27 20 64 6f 77  |on to 'dive' dow|
000000d0  6e 20 61 20 66 75 6e 63  74 69 6f 6e 5c 6e 22 29  |n a function\n")|
000000e0  3b 0a 20 20 20 20 20 20  20 20 70 72 69 6e 74 66  |;.        printf|
000000f0  28 22 74 6f 20 61 20 63  65 72 74 61 69 6e 20 6c  |("to a certain l|
00000100  65 76 65 6c 2c 20 74 68  65 6e 20 72 65 74 75 72  |evel, then retur|
00000110  6e 20 73 61 66 65 6c 79  2e 5c 6e 5c 6e 22 29 3b  |n safely.\n\n");|
00000120  20 0a 20 20 20 20 20 20  20 20 64 69 76 65 28 31  | .        dive(1|
00000130  29 3b 0a 7d 0a 0a 76 6f  69 64 0a 64 69 76 65 28  |);.}..void.dive(|
00000140  69 6e 74 20 78 29 0a 7b  0a 20 20 20 20 20 20 20  |int x).{.       |
00000150  20 69 66 20 28 78 20 3d  3d 20 35 29 20 7b 0a 20  | if (x == 5) {. |
00000160  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 70  |               p|
00000170  72 69 6e 74 66 28 22 52  65 61 63 68 65 64 20 74  |rintf("Reached t|
00000180  75 72 6e 69 6e 67 20 70  6f 69 6e 74 3b 20 6e 6f  |urning point; no|
00000190  77 20 77 65 20 77 6f 72  6b 20 62 61 63 6b 20 74  |w we work back t|
000001a0  68 72 6f 75 67 68 20 61  6c 6c 5c 6e 22 29 3b 0a  |hrough all\n");.|
000001b0  20 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |                |
000001c0  70 72 69 6e 74 66 28 22  20 20 20 74 68 65 20 66  |printf("   the f|
000001d0  75 6e 63 74 69 6f 6e 73  20 77 65 27 76 65 20 6a  |unctions we've j|
000001e0  75 73 74 20 63 61 6c 6c  65 64 2e 5c 6e 22 29 3b  |ust called.\n");|
000001f0  0a 20 20 20 20 20 20 20  20 20 20 20 20 20 20 20  |.               |
00000200  20 72 65 74 75 72 6e 3b  0a 20 20 20 20 20 20 20  | return;.       |
00000210  20 7d 0a 20 20 20 20 20  20 20 20 70 72 69 6e 74  | }.        print|
00000220  66 28 22 52 75 6e 6e 69  6e 67 20 66 6f 72 77 61  |f("Running forwa|
00000230  72 64 73 20 2d 20 6c 65  76 65 6c 20 25 64 5c 6e  |rds - level %d\n|
00000240  22 2c 20 78 29 3b 0a 20  20 20 20 20 20 20 20 64  |", x);.        d|
00000250  69 76 65 28 78 2b 31 29  3b 0a 0a 20 20 20 20 20  |ive(x+1);..     |
00000260  20 20 20 2f 2a 20 54 68  69 73 20 69 73 20 77 68  |   /* This is wh|
00000270  65 72 65 20 63 6f 6e 74  72 6f 6c 20 72 65 74 75  |ere control retu|
00000280  72 6e 73 20 6f 6e 63 65  20 74 68 65 20 66 75 6e  |rns once the fun|
00000290  63 74 69 6f 6e 20 62 65  6c 6f 77 20 75 73 0a 20  |ction below us. |
000002a0  20 20 20 20 20 20 20 20  20 20 68 61 73 20 66 69  |          has fi|
000002b0  6e 69 73 68 65 64 20 65  78 65 63 75 74 69 6e 67  |nished executing|
000002c0  20 2a 2f 0a 0a 20 20 20  20 20 20 20 20 70 72 69  | */..        pri|
000002d0  6e 74 66 28 22 52 75 6e  6e 69 6e 67 20 62 61 63  |ntf("Running bac|
000002e0  6b 77 61 72 64 73 20 2d  20 6c 65 76 65 6c 20 25  |kwards - level %|
000002f0  64 5c 6e 22 2c 20 78 29  3b 0a 7d 0a              |d\n", x);.}.|
000002fc