Micro Mart


Functional

 
Published in Micro Mart #1195

This week, Shaun has some more information about functions in C

Retro: Functional

This week, we'll be taking a further look at function calls. If you remember last week's column, you'll have noticed that the scroll function was of type void. In other words, while it was manipulating our string, it didn't take any arguments to do so, nor did it return any value, although it manipulated the variable used (which we called text[ ]) as this was declared globally, so that became available to the whole program. For our purposes, global variables (sometimes referred to as class variables or static fields) are fine, and from a personal perspective, I prefer them.

If you venture into object-orientated programming, however, you may come across private, public and protected variables. I won't be covering this, as it's not necessary. What we're going to do this week is to set up three simple mathematics functions that perform arithmetic for us. We'll call the functions addition, multiplication and subtraction which will take whole numbers. Here is this week's code:

	/* Maths.c */
	#include 
	#include 
	
	// Function prototypes:
	static void main (void);
	static int addition (int, int);
	static int multiplication (int, int);
	static int subtraction (int, int);

	// Global variables:
	static int x = 0;
	static int y = 0;

	void main ()
	{
		// Sets initial variables for function call:
		x = -15; y = 33;

		printk ("%c",12);  // Clears screen
		printk ("%d + %d = %d\n\n", x, y, addition (x, y));

		// We want the 12 times table, so:
		y=12;

		//\x1\x20 will switch to 32 columns mode:
		printk ("\x1\x20Twelve times table:\n");
		
		for(x =0; x< 13; x = x+1)
		{
			// %03d will print decimal numbers with leading zeros if
			// two or less digits long:

			printf (" %03d x %03d = %03d \n" x, y, multiplication (x, y));

			// Switches back to 64 columns by \x1\x40

			printk ("\W1\x40\nNumbers sent directly to subtraction function:\n");
			printk ("10 - 12 = %d \n", subtraction (10, 12));

		}
		int addition (int a, int b)
		{
			return a + b;
		}
		int multiplication (int a, int b)
		{
			return a * b;
		}
		int subtraction (int a, int b)
		{
			return a - b;
		}
	}

Save this as Maths.c and compile it with:

zcc +zx -Indos -create-app -o Maths Maths.c

You'll note that there are two print commands within the code, printf and printk. The difference is that printk is streamlined and doesn't accept all of the formatting commands that printf does, such as number padding. For instance, %04d will pad out any number that does not have four digits with leading zeros, so 0001 will appear rather than simply 1. This is quite useful, as you will find out.

After storing the numbers that you want in x and y, you are then sending each to the relevant function to do the work. Locally, as in within the function calls, these are passed into the variables a and b, which are then computed and the result is returned.

The variables used are of type integer (int for short), which are whole numbers and have a range of two bytes or 16-bits, representing values between -32767 to +32768 signed or zero to 65535 unsigned. We are using signed numbers here.

The functions used are not too dissimilar to the way that the DEF FN command works in Sinclair BASIC (tinyurl.com/SinclairDEF-FN), but as you know from previous weeks, your function can be more than purely mathematical.

Remembering that computers can not divide by zero, add a function that does simple division, or change the functions to take in more arguments, or see if you can use two or more functions in one line of code, for instance, adding two numbers and multiplying by the result of two others. I'll speak further about on the relevant thread over at forum.micromart.co.uk. Head over there or contact me with your questions to Shaun@micromart.co.uk.

Shaun Bebbington