Download C Programming Functions: Definition, Declaration, Invocation, Recursion, and Preprocessor and more Study notes Electrical and Electronics Engineering in PDF only on Docsity! Functions Purpose: functions break large computing tasks into smaller ones, and enable programmers to use routines that others have written. I. 3 Aspects of Functions A. Definition B. Declaration/Prototype C. Invocation/Call II. Function Definition A. Syntax return-type function-name (argument-list) { declarations and statements } B. Properties 1. The return-type, argument-list, and declarations and statements may all be omitted. The function-name must be present. Example: dummy ( ) { } This function does nothing and returns nothing. This kind of function is useful during program development. 2. If the return-type is omitted, the default return type is int. 3. Communication between functions is by the arguments and values returned by the function. 4. Functions may occur in any order in a source file. They may be spread out among several source files. 5. A function definition may not be enclosed within another function definition. 6. The return statement is the mechanism for returning a value from the called function to the caller. return expression; Both the return statement and the expression are optional. 7. If there is no expression after the return statement, the function returns a “value” which is unknown (garbage). If the return-type of the function is void, then there must not be an expression following the return statement. 8. The calling function is free to ignore the return value. B. Example 1 void mess(int n) { 2 int i; 3 for (i=0; i<=n; ++i) 4 printf("Go Coogs!\n"); 5 } 1. Line 1 of the example shows the “header” of the function definition. 2. Line 2 gives the local variable declarations. 3. Lines 3 and 4 show the local statements. C. Another Example: Newton-Raphson 2 ( ) [ ( )] 1 1 ( ) ( ) ( ) ( ) ( ) 1! 2!= = = = + − = + − + − +′ ′′ a a a a a a ax x x x x x f x f x x x f x f x x x f x x x … If 1− ax x = , then ( ) ( ) ( ) ( )= =≅ + −′a a ax x x xf x f x f x x x If we wish to solve ( ) 0=f x , then we can solve the above equation for x yielding ( ) ( ) = = ≅ − ′ a a x x a x x f x x x f x To solve, make an initial guess for xa and solve for x. If ( )f x is sufficiently close to zero, then the solution has been reached. Otherwise, replace xa with x and repeat. The process is repeated until the solution is reached or a maximum number of iterations is performed. 1 double nr(double x, double eps, int maxiter) { 2 int iter; 3 double xa; 4 double f(double x); 5 double f1(double x); 6 for (iter=1; iter<=maxiter; ++iter) { 7 xa = x; 8 x = xa - f(xa)/f1(xa); 9 if (fabs(x-xa) <= eps) break; 10 } 11 if (iter > maxiter) { 12 printf("Too many iterations\n"); 13 return -999.0; 14 } 15 else 16 return x; 17 } 1. Again, notice the function “header” in line 1. 2. Lines 1-17 comprise the function definition. 3. Notice the two function declarations on lines 4 and 5. 4. Notice the two function invocations on line 8. 5. If line 13 is reached, there will be an immediate return to the calling function. No other statements in that function are executed. II. Function Declaration/Prototype A. Functions have a scope defined by the location of their declaration. E. Another example: Fibonacci sequence int fib(int n) { /* computes the value of the nth position in a Fibonacci sequence */ if (n > 2) return fib(n-1) + fib(n-2); else if (n==1 || n==2) return 1; else return 0; } long fact(int); int fib(int); void main() { long n=6; n = fact(fib(n)); n = ???; } Preprocessor Purpose: provides a separate first s tep in compilation I. Preprocessor directives - commands that are “executed” prior to the source code being compiled. II. Two most commonly used A. #define B. #include III. Macro Substitution (#define) A. Syntax #define name replacement-text Notice that there is no semi -colon at the end of the line. This is because this is not a statement, it is a preprocessor directive. B. Examples #define PI 3.141592653589793238462643383279502884197 #define rtd(rad) ((rad)*180.0/PI) #define dtr(deg) ((deg)*PI/180.0) III. File Inclusion (#include) A. Syntax #include “filename” or #include <filename> B. When the filename is enclosed in quotation marks, the search for the file begins in the directory where the program is located. If the file is not found, then the search may continue in other directories based on the implementation. C. When the filename is enclosed in angled brackets, the search for the file begins at an implementation defined location. D. Some common standard libraries included in this class are: <stdio.h> Input and output <ctype.h> Character functions and tests <string.h> String manipulation functions <stdlib.h> Utility functions (e.g. requests for memory allocation) <stdarg.h> Variable argument lists <time.h> Time and date functions <limits.h> Implementation defined limits E. Contrary to popular belief, <math.h> is not a standard C library although it is provided in many implementations. For the UNIX operating system, please read the compiler documentation on how to include the math library.