Function Definitions and Declarations ------------------------------------- The purpose of a C function, whether library or user-written, is to receive data, operate on the data and directly return at most a single value. One more time: input parameters --> function --> output at most 1 value We have been using a function called main() all along. We have also used other functions such as atoi(), printf(), scanf() etc... These functions are predefined. Now it is time to create out own functions. This is done by the following format: data_type function_name(input parameters) { body of function goes here } For example: int absolute_value(int a) { if(a>0) return a; else return -a; } This function returns an integer value and is named absolute_value and reads in one integer argument. It then performs the absolute value operation and returns the positive integer value. Naming a function involves following the same rules as defined earlier for variables. No keywords are allowed, function names must start with a letter, combinations of letters and numbers may be used. Parenthesis are used to delimit the function arguments. Examples: foo() flop_pie(char a, int b) do_it_now(int hi) There is an additional data type that needs to be discussed. That type is "void". When used as the return type, it means "don't return a value". When void is used as the return type, the function essentially becomes a procedure. A procedure does not return a value, a function does. For example: void perform_this_long_operation() { long operation goes in here } Other examples: int apple(char yy, char y[2], int a) { function main body goes here } The last example is a function since it returns an integer value (no void there). Notice that you may also input arrays to functions. Here is an example program that determines the sign of a number. The mathematical function is defined as: sgn(x) = {1 , x > 0; -1 , x < 0; 0 , otherwise} The C code is: int sgn(int x) { if(x > 0) return 1; else if(x < 0) return -1; else return 0; } main() { int x; int signval; scanf("%d", &x); signval = sgn(x); } Notice the call to the function is "sgn(x)", you are just sending the variable x into the function "sgn()". The function definition itself has a return value of type int, an input parameter of type int (must be declared) and returns an integer value using the return command. Here is a function that computes the max of 2 numbers: float find_max(float x, float y) { if(x > y) return x; else return y; } Quite simple, right? To call this function, in the main function add the line: max = find_max(a,b); Make sure max, a and b are floating point. Here is a sample program to read in a sequence of numbers and then display them using one procedure and one function: #include /* declare the functions globally at the top with prototypes */ int get_numbers(float nums[10]); void print_numbers(float nums[10], int num); /* function definitions */ int get_numbers(float nums[10]) /* 10 max allowed */ { /* stop when -99.0 is entered */ int i = -1; do { ++i; scanf("%f", &nums[i]); } while(nums[i] != -99.0 && i<9) return i; /* returning the total number of numbers entered */ } void print_numbers(float nums[10], int num) { /* output all num numbers */ int i; for(i=0;i