#include #include /* Demonstration of function calls in C. Compiles on a UNIX system using gcc -lm -Wall -o C-examples-1 C-examples-1.c Execute from the shell using: "C-examples-1". If that doesn't work (because '.' is not in your PATH) then you'll have to type: "./C-examples-1". */ /***************************************************************************** Functions that take no values and return no values. ******************************************************************************/ /* A function that does nothing but print "Hello World". */ void hello_world() { printf("Hello World\n"); } /***************************************************************************** Functions that take values but return no values. ******************************************************************************/ /* A function that takes an integer argument and prints its square. */ void print_square(const int x) { printf("%d^2 = %d\n", x, x * x); } /* A function that takes two integer arguments and prints their sum. */ void print_sum(const int x, const int y) { printf("%d + %d = %d\n", x, y, x + y); } /***************************************************************************** Functions that both take and return values. ******************************************************************************/ /* A function that returns the square of its integer type argument. */ int square(const int x) { return x * x; } /* A function that returns the sum of its two integer type arguments. */ int sum(const int x, const int y) { return x + y; } /* Integer division. Note that the quotient of integers is an integer. */ int divide_int(const int x, const int y) { return x / y; } /* Floating-point division is more often what is needed. */ double divide_double(const double x, const double y) { return x / y; } /* This forces the floating point quotient by "casting" the integer values to floating point values. */ double divide_int_as_double(const int x, const int y) { return (double)x / (double)y; } /***************************************************************************** Functions that take values and modify them. ******************************************************************************/ /* This function adds one to it. The "*" indicates that we are taking a pointer to the argument, meaning that changes to "x" inside the function affect the actual argument's value. */ void add_one_in_place(int* x) { *x += 1; } /* This useless function makes a copy of x and adds one to it. This does nothing to the actual argument because no reference was taken. This function calls its arguments "by value" rather than "by reference" (the default for scalar variables in C). In call by value, a copy of the argument is made inside the function so that changes to the argument inside of the function do not effect the actual argument. */ void do_nothing(int x) { x += 1; } /****************************************************************************** main() is caled when the program is executed. Other functions in this file are only called through main(). ******************************************************************************/ int main(int argc, char** argv) { /* Variable declarations */ int z, x, g, y; /* This is the first statement to be executed. */ printf("Beginning of main()\n"); /* Call the hello_world function. */ hello_world(); /* Our first function with an argument. */ print_square(3); print_square(13); /* A function with two arguments. */ print_sum(3, 11); print_sum(37, 54); /* A function that returns a value. */ z = square(4); printf("4^2 = %d\n", z); /* We may declare and assign simultaneously. */ x = square(3); printf("3^2 = %d\n", x); /* Functions may be composed. */ g = square(square(square(2))); printf("((2^2)^2)^2 = %d\n", g); printf("3^2 + 4^2 = %d\n", sum(square(3), square(4))); /* Integer division. */ printf("3 / 2 = %d in integer division.\n", divide_int(3, 2)); /* Floating point division. */ printf("3 / 2 = %f in floating point division.\n", divide_double(3, 2)); /* Division of integers casted to floating point values. */ printf("3 / 2 = %f (integers casted to floating point values).\n", divide_int_as_double(3, 2)); /* Demonstration of pointer arguments. */ y = 3; printf("\ny = %d (before)\n", y); add_one_in_place(&y); printf("y = %d (after)\n", y); printf("\ny = %d (before)\n", y); do_nothing(y); printf("y = %d (after)\n", y); return 0; }