



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth explanation of functions in c programming, including their definition, declaration, invocation, recursion, and the use of the preprocessor. It covers aspects such as function syntax, properties, communication between functions, function declaration and prototype, function invocation, and recursion. The document also includes examples of function definitions and uses of the preprocessor directives #define and #include.
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




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
2
= (^) a + ′ (^) a − + ′′ a − +
a a x x x x a^ x x a
f x f x x x f x f x x x f x x x K
If x − xa = 1 , then f ( ) x ≅ f ( ) x (^) x = xa + f ′( ) x (^) x = xa ( x − xa ) If we wish to solve f ( ) x = 0 , 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 }
D. Example: 1 double fun(int, float); 2 void main() { 3 float y = 5.4; 4 double z; 5 z = fun(7, y+2); 6 fun = fun(7, y+2); /* Illegal! / 7 } 8 9 double fun(int n, float x) { 10 return xx+n; 11 }
IV. Recursion A. A function may call itself directly or indirectly. B. Direct recursive calls are the most common and easiest to understand. C. Be very careful of indirect recursive calls, as they tend to be difficult to trace and debug. D. If it is possible to write a function as non-recursive, this is generally preferred since it is easier to debug and tends to have faster execution speeds. D. Example: Factorial (recursive and non-recursive versions) long fact(int n) { /* computes the factorial of n (recursive version) / if (n > 0) return nfact(n-1); else if (n == 0) return 1; else return 0; }
long fact(int n) { /* computes the factorial of n (non-recursive version) */ long value = 1; int i; if (n < 0) return 0; for (i=2; i<=n; ++i) value *= i; return value; }
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 = ???; }
Purpose: provides a separate first step 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. #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 >