View Single Post
Old Jan 21st, 2006, 1:15 PM   #8
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by sixstringartist
Is this similar to creating functions? I have past experience using matlab but I dont konw how to write functions in C.
You do know how to write functions in C - you just don't know it.

All code in C is encapsulated in a function. The first function called when a program executes is the function called "main".

If we take a look at the main function in your code:
int main()
{
    // Your code
}
The first keyword we come across is "int". This denotes the return value of the function. The main function should always return an int, which the operating system uses as the error code. If main returns 0, all is well. If it does not, an error has occurred, and the number that is returned may give a clue as to exactly what went wrong.

The second bit in the function is "main", which is the function name. Then "()". This empty pair of brackets tells C that this function has no arguments. Finally, the "{}" brackets encompass your code and tell C where the function starts, and where it ends.

A prime function could look like this:
#define TRUE  1
#define FALSE 0

int is_prime(int number)
{
	int i;

	for (i = 2; i < number; i++)
	{
		if ((number % i) == 0)
		{
			return FALSE;
		}
	}
	return TRUE;
}
Note this function takes in an integer as an argument, and returns FALSE (0) if it's not a prime, and TRUE (1) if it is.

We can call this function from main() like so:
int main()
{
	int input;

	printf("Please enter an integer to test: ");
	scanf("%d", &input);

	if (is_prime(input))
	{
		printf("That number is prime.\n");
	}
	else
	{
		printf("That number is not prime.\n");
	}
	
	return 0;
}
There is also the issue of prototyping. Before a function in C can be used, the compiler must know about it. Thus, the following is legal:
int function() {
    return 0;
}
int main() {
    function();
}
But the next bit of code is not:
int main() {
    function();
}
int function() {
    return 0;
}
This is because "function" is referenced in main() before it is defined. You can get around this by adding in a function prototype. A prototype says, "This is what the function's inputs and outputs are, but I'll define the actual code later on":
int function();

int main() {
    function();
}
int function() {
    return 0;
}
In general, it's best to prototype functions in a header file. This allows functions in one file, to reference functions in another file, if both files include the same header.

Just for reference, a whole prime program could look something like the example I give below. Note it doesn't have the same input checking as yours, but it shouldn't be hard to put that in
// prime.h
#ifndef PRIME_H
#define PRIME_H

int is_prime(int number);

#endif
// prime.c
#include <stdio.h>
#include "prime.h"

#define TRUE  1
#define FALSE 0

int is_prime(int number)
{
	int i;

	for (i = 2; i < number; i++)
	{
		if ((number % i) == 0)
		{
			return FALSE;
		}
	}
	return TRUE;
}

int main()
{
	int input;

	printf("Please enter an integer to test: ");
	scanf("%d", &input);

	if (is_prime(input))
	{
		printf("That number is prime.\n");
	}
	else
	{
		printf("That number is not prime.\n");
	}
	
	return 0;
}
Arevos is offline   Reply With Quote