C Programming Functions: Definition, Declaration, Invocation, Recursion, and Preprocessor, Study notes of Electrical and Electronics Engineering

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

Pre 2010

Uploaded on 08/18/2009

koofers-user-fdv
koofers-user-fdv 🇺🇸

9 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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 }
pf3
pf4
pf5

Partial preview of the text

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

( ) [ ( )]

= (^) 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 xxa = 1 , then f ( ) xf ( ) x (^) x = xa + f ′( ) x (^) x = xa ( xxa ) 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 }

  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.

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 = ???; }

Preprocessor

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 >