ECE 246: INTRODUCTION TO C PROGRAMMING

Chapter 4: FUNCTIONS

Learning Objectives
     

To construct programs modularly from small pieces called functions. The common math functions available in the C Standard Library. To create new functions. The mechanisms used to pass information between functions. Simulation techniques using random number generation. How to write and use recursive functions, i.e., functions that call themselves.

Lesson Outline

 

Void Functions Call by value Call by reference

Introduction

Divide and conquer

Construct a program from smaller pieces or components

These smaller pieces are called modules

Each piece more manageable than the original program

does task.Program Modules in C  Functions   Modules in C Programs combine user-defined functions with library functions  C standard library has a wide variety of functions  Function calls  Invoking functions    Provide function name and arguments (data) Function performs operations or manipulations Function returns results Boss asks worker to complete task    Function call analogy:  Worker gets information. returns result Information hiding: boss does not know details .

When possible. This can reduce program development time.Good Programming Practices   Familiarize yourself with the rich collection of functions in the C Standard Library. Avoid reinventing the wheel. use C Standard Library functions instead of writing new functions. .

.Hierarchical boss function/worker function relationship.

sqrt( 900. which returns the square root of its argument All math functions return data type double  Arguments may be constants. variables. use comma-separated list printf( "%.2f".  If multiple arguments.h> FunctionName( argument ).Math Library Functions  Math library functions   perform common mathematical calculations #include <math. or expressions .   Format for calling functions    Calls function sqrt.0 ) ).

.h> when using functions in the math library.Error-Prevention Tip  Include the math header by using the preprocessor directive #include <math.

0 ceil( x ) rounds x to the smallest integer ceil( 9.0 ) is 5.718282 exp( 2.0 ) is 2.0 fabs( x ) absolute value of x fabs( 5.0 log( 7.2 ) is 10.Function sqrt( x ) Description square root of x Example sqrt( 900.0 fabs( 0.0 Commonly used math library functions.8 ) is -9.) .0 ) is 30.0 ) is 5.718282 ) is 1.0 exp( x ) exponential function ex exp( 1.0 fabs( -5.0 ) is 2.0 log10( 10.0 ) is 3.0 not less than x ceil( -9.0 ) is 7.389056 log( x ) natural logarithm of x (base e) log( 2.0 sqrt( 9. (Part 1 of 2.0 log10( x ) logarithm of x (base 10) log10( 1.389056 ) is 2.0 ) is 0.0 ) is 0.0 ) is 1.0 log10( 100.

333 ) is 1. y ) remainder of x/y as a floatingpoint number trigonometric sine of x (x in radians) trigonometric cosine of x (x in radians) trigonometric tangent of x (x in radians) fmod( 13.5 ) is 3.) .0 pow( x.657.2 ) is 9. y ) fmod( x. .0 floor( -9.8 ) is -10.Function floor( x ) Description rounds x to the largest integer not greater than x x raised to power y (xy) Example floor( 9.0 ) is 0.0 tan( x ) tan( 0. 2. (Part 2 of 2.0 cos( x ) cos( 0.0 Commonly used math library functions.0 ) is 0.992 sin( x ) sin( 0.0 pow( 2.0 ) is 1.0 pow( 9. 7 ) is 128.

hide internal details (library functions) Software reusability    Avoid code repetition .Functions  Functions   Modularize a program All variables defined inside functions are local variables  Known only in function defined  Parameters   Communicate information between functions Local variables  Benefits of functions   Divide and conquer  Manageable program development Use existing functions as building blocks for new programs Abstraction .

main is often implemented as a group of calls to functions that perform the bulk of the program‘s work.Software Engineering Observations    In programs containing many functions. and the function name should effectively express that task. well-defined task. it is possible that your function is attempting to perform too many diverse tasks. This facilitates abstraction and promotes software reusability. Each function should be limited to performing a single. If you cannot choose a concise name that expresses what the function does. It is usually best to break such a function into several smaller functions. .

5 Function Definitions  Function definition format return-value-type function-name( parameter-list ) { declarations and statements }   Function-name: any valid identifier Return-value-type: data type of the result (default int)  void – indicates that the function returns nothing  Parameter-list: comma separated list. declares parameters .5.

  or.Function Definitions  Function definition format (continued) return-value-type function-name( parameter-list ) { declarations and statements }  Definitions and statements: function body (block)   Variables can be defined inside blocks (can be nested) Functions can not be defined inside other functions If nothing returned   Returning control  return. until reaches right brace return expression. If something returned  .

.Good Programming Practice  Place a blank line between function definitions to separate the functions and enhance program readability.

square( x ) ). 18 19 return 0.c Creating and using a programmer-defined function */ #include <stdio.1 2 3 4 5 6 7 /* Fig. /* indicates successful termination */ 20 21 } /* end main */ Call to square function 22 23 /* square function definition returns square of parameter */ 24 int square( int y ) /* y is a copy of argument to function */ 25 { 26 return y * y. /* function prototype */ /* function main begins program execution */ Function prototype indicates function 8 int main( void ) 9 { be defined later in the program 10 int x. x <= 10. x++ ) { printf( "%d ". /* counter */ 11 12 /* loop 10 times and calculate and output square of x each time */ 13 14 15 16 for ( x = 1. /* returns square of y as an int */ 27 28 } /* end function square */ 1 4 9 16 25 36 49 64 81 100 Function definition . 5. /* function call */ } /* end for */ will 17 printf( "\n" ).h> int square( int y ).3: fig05_03.

Returning a value from a function with a void return type is a syntax error. . Forgetting to return a value from a function that is supposed to return a value can lead to unexpected errors.Common Programming Errors    Omitting the return-value-type in a function definition is a syntax error if the function prototype specifies a return type other than int. The C standard states that the result of this omission is undefined.

Defining a function parameter again as a local variable within the function is a syntax error. y would actually make y a parameter of type int because int is the default. double y might cause errors in your programs. y instead of double x. The parameter declaration double x. .Tips & Observations     Even though an omitted return type defaults to int. Specifying function parameters of the same type as double x. always state the return type explicitly. Placing a semicolon after the right parenthesis enclosing the parameter list of a function definition is a syntax error.

Good Programming Practices     Include the type of each parameter in the parameter list. even if that parameter is of the default type int. This helps avoid ambiguity. Defining a function inside another function is a syntax error. Choosing meaningful function names and meaningful parameter names makes programs more readable and helps avoid excessive use of comments. do not use the same names for the arguments passed to a function and the corresponding parameters in the function definition. Although it is not incorrect to do so. .

functions should generally be no longer than half a page. and order of arguments and parameters. Consider dividing the function into smaller functions that perform the separate tasks. Small functions promote software reusability. type.Software Engineering Observations     A function should generally be no longer than one page. function header and function calls should all agree in the number. debug. maintain and modify. . Programs should be written as collections of small functions. Better yet. A function requiring a large number of parameters may be performing too many tasks. and in the type of return value. The function header should fit on one line if possible. This makes programs easier to write. The function prototype.

number3 ) ). /* indicates successful termination */ Function prototype Function call 23 } /* end main */ . 5. /* number1. return 0.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 24 /* Fig. &number1. int y. /* third integer */ printf( "Enter three integers: " ). &number2. /* first integer */ int number2. number2. scanf( "%d%d%d". number2 and number3 are arguments to the maximum function call */ printf( "Maximum is: %d\n". maximum( number1.c Finding the maximum of three integers */ #include <stdio. /* function prototype */ /* function main begins program execution */ int main( void ) { int number1.h> int maximum( int x. /* second integer */ int number3. int z ). &number3 ).4: fig05_04.

} /* end if */ int max = x. } /* end if */ if ( z > max ) { /* if z is larger than max. /* assume x is largest */ Function definition 39 return max.25 /* Function maximum definition */ 26 /* x. y and z are parameters */ 27 int maximum( int x. assign y to max */ max = y. int y. int z ) 28 { 29 30 31 32 33 34 35 36 37 38 if ( y > max ) { /* if y is larger than max. assign z to max */ max = z. /* max is largest value */ 40 41 } /* end function maximum */ Enter three integers: 22 85 17 Maximum is: 85 Enter three integers: 85 22 17 Maximum is: 85 Enter three integers: 22 17 85 Maximum is: 85 .

int z ).Function Prototypes  Function prototype       Function name Parameters – what the function takes in Return type – data type function returns (default int) Used to validate functions Prototype only needed if function definition comes after use in program The function with the prototype   int maximum( int x. int y. Takes in 3 ints Returns an int  Promotion rules and conversions  Converting to lower types can lead to errors .

Good Programming Practices

Include function prototypes for all functions to take advantage of C‘s type-checking capabilities. Use #include preprocessor directives to obtain function prototypes for the standard library functions from the headers for the appropriate libraries, or to obtain headers containing function prototypes for functions developed by you and/or your group members. Parameter names are sometimes included in function prototypes (our preference) for documentation purposes. The compiler ignores these names. Forgetting the semicolon at the end of a function prototype is a syntax error.

Data type
Long double double float Unsigned long int long int unsigned int int unsigned short short char

printf conversion specification
%Lf %f %f %lu %ld %u %d %hu %hd %c

scanf conversion specification
%Lf %lf %f %lu %ld %u %d %hu %hd %c

Promotion hierarchy for data types.

Common Programming Errors

Converting from a higher data type in the promotion hierarchy to a lower type can change the data value. Forgetting a function prototype causes a syntax error if the return type of the function is not int and the function definition appears after the function call in the program. Otherwise, forgetting a function prototype may cause a runtime error or an unexpected result.

Software Engineering Observation

A function prototype placed outside any function definition applies to all calls to the function appearing after the function prototype in the file. A function prototype placed in a function applies only to calls made in that function.

it is placed on top of the stack  When a function ends.Function Call Stack and Activation Records  Program execution stack  A stack is a last-in. first-out (LIFO) data structure  Anything put into the stack is placed ―on top‖  The only data that can be taken out is the data on top  C uses a program execution stack to keep track of which functions have been called  When a function is called. it is taken off the stack and control returns to the function immediately below it  Calling more functions than C can handle at once is known as a ―stack overflow error‖ .

h" Reuse functions .h> .h> . <math.h Load in other files with #include "filename. etc Load with #include <filename> #include <math.h>  Custom header files     Create file with functions Save as filename.Headers  Header files    Contain function prototypes for library functions <stdlib.

Defines macros that are useful for reporting error conditions.h> <float. The notion of locale enables the computer system to handle different conventions for expressing data like dates.h> Contains macros and information for adding diagnostics that aid program debugging. Contains the integral size limits of the system.h> <ctype. Contains function prototypes and other information that enables a program to be modified for the current locale on which it is running. Contains the floating-point size limits of the system.Standard library header Explanation <assert. and function prototypes for functions that can be used to convert lowercase letters to uppercase letters and vice versa.h> <limits.h> <locale. (Part 1 of 3.h> Some of the standard library headers. Contains function prototypes for functions that test characters for certain properties.) . <errno. dollar amounts and large numbers throughout the world. times.

h> <stdarg.h> <stddef. Some of the standard library headers.h> <signal. Contains function prototypes and macros to handle various conditions that may arise during program execution. Contains common definitions of types used by C for performing certain calculations.h> Contains function prototypes for math library functions. (Part 2 of 3.Standard library header Explanation <math. Defines macros for dealing with a list of arguments to a function whose number and types are unknown.h> <setjmp. Contains function prototypes for functions that allow bypassing of the usual function call and return sequence.) .

(Part 3 of 3. <string. and information used by them. memory allocation.) .h> <time. and other utility functions. Contains function prototypes and types for manipulating the time and date.Standard library header Explanation <stdio. Contains function prototypes for conversions of numbers to text and text to numbers. random numbers.h> Some of the standard library headers. Contains function prototypes for string-processing functions.h> Contains function prototypes for the standard input/output library functions.h> <stdlib.

Calling Functions: Call-by-Value  Call by value    Copy of argument passed to function Changes in function do not effect original Use when function does not need to modify argument  Avoids accidental changes (All of our previous examples are from this type) .

the called function can access and change the value stored there directly .Calling Functions: Call-by-Reference  Call by reference     Used when it is necessary to give a called function direct access to the variables of its calling function The called function can use and change the value of defined variables The address of the variable is passed to the called function By knowing the address/location.

passing addresses is referred to as a function pass/call by reference Can return multiple values Note: C only supports call-by-value . the called function can reference or access the variable whose address has been passed Thus.Calling Functions: Call-by-Reference     In summary.

the above example means: num1 is the address of a floating point value Example if 2 addresses are required: float& num1. Since & (ampersand) means: ‗the address of‘.Call-by-Reference: Function Definition Format return-value-type function-name( parameter-list ) { declarations and statements }     For parameter-list: data-type& reference-name Example: float& num1. float& num2 .

/*prototype with 2 reference parameters*/ int main() { float firstnum.h> void newval(float&. } .firstnum).&firstnum). scanf(“%f”. printf(“The value in firstnum is: %f\n”.#include <stdio. secnum).secnum). /*call the function */ printf(“The value in firstnum is now: %f\n”.secnum). scanf(“%f”. printf(“Enter two numbers:\n”).&secnum). return 0. newval(firstnum.firstnum). secnum. printf(“The value in secnum is: %f\n”. float&). printf(“The value in secnum is now: %f\n”.

xnum). printf(“Thevalue in ynum is: %f\n”.5 The value in ynum is: 33 The value in firstnum is now: 89.0 The value in firstnum is: 22.5 The value in secnum is now: 99. float& ynum).5. return.5 The value in secnum is: 33 The value in xnum is: 22.5. } Enter two numbers: 22.ynum).5 . xnum=89. { printf(“Thevalue in xnum is: %f\n”.5 33.void newval(float& xnum. ynum=99.

ynum) Parameters(xnum.secnum) can be altered from within newval() by using the parameter names (xnum.ynum) don’t store copies of the values (firstnum. secnum and xnum.Codes explanation    firstnum.ynum refer to the same data items The values in the arguments(firstnum.secnum) but directly access the locations in memory set for these 2 arguments .

the values initially displayed for the parameters xnum and ynum are the same as those displayed for the arguments firstnum and secnum Since (xnum.ynum) are reference parameters. newval() has direct access to the arguments firstnum and secnum Any change to xnum within newval() directly alters the value of firstnum in main() and any change to ynum directly changes secnum‘s value .Codes explanation    As in output.

calculate the total resistance in parallel and display the result. Then.Quiz Write a C program which requires user inputs of two values of resistance. . Make sure you include the appropriate preprocessor commands and comments.

MORE EXAMPLES of functions .

1   Add 1 to make random number between 1 and n   1 + ( rand() % 6) number between 1 and 6 .Random Number Generation  rand function    Load <stdlib.h> Returns "random" number between 0 and RAND_MAX (at least 32767)  i = rand(). Pseudorandom   Preset sequence of "random" numbers Same sequence for every function call  Scaling  To get a random number between 1 and n  1 + ( rand() % n ) rand() % n returns a number between 0 and n .

i <= 20.c Shifted. /* indicates successful termination */ 25 26 } /* end main */ 6 5 6 6 6 1 6 2 5 1 2 3 5 5 4 4 6 3 2 1 . /* counter */ /* loop 20 times */ for ( i = 1.7: fig05_07.h> #include <stdlib. begin new line of if ( i % 5 == 0 ) { printf( "\n" ). } /* end if */ Generates a random number between 1 output */ and 6 22 } /* end for */ 23 24 return 0. 5. scaled integers produced by 1 + rand() % 6 */ #include <stdio. 1 + ( rand() % 6 ) ).1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /* Fig. /* if counter is divisible by 5. i++ ) { /* pick random number from 1 to 6 and output it */ printf( "%10d".h> /* function main begins program execution */ int main( void ) { int i.

break.h> 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #include <stdlib.h> /* function main begins program execution */ int main( void ) { int frequency1 = 0.8: fig05_08. /* rolled 6 counter */ int roll. roll <= 6000. /* rolled 4 counter */ int frequency5 = 0. /* rolled 1 counter */ int frequency2 = 0. . /* roll counter. roll++ ) { face = 1 + rand() % 6. /* represents one roll of the die. 5. value 1 to 6 */ /* loop 6000 times and summarize results */ for ( roll = 1. /* rolled 3 counter */ int frequency4 = 0.c 2 Roll a six-sided die 6000 times */ 3 #include <stdio. /* random number from 1 to 6 */ /* determine face value and increment appropriate counter */ switch ( face ) { case 1: /* rolled 1 */ ++frequency1. /* rolled 2 counter */ int frequency3 = 0. value 1 to 6000 */ int face./* Fig. /* rolled 5 counter */ int frequency6 = 0.

case 5: /* rolled 5 */ ++frequency5. break. break. break.30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 case 2: /* rolled 2 */ ++frequency2. break. /* optional */ } /* end switch */ } /* end for */ . break. case 4: /* rolled 4 */ ++frequency4. case 6: /* rolled 6 */ ++frequency6. case 3: /* rolled 3 */ ++frequency3.

). 62 return 0. printf( " printf( printf( printf( printf( printf( " " " " " 1%13d\n". ). ). "Frequency" ). ). frequency2 frequency3 frequency4 frequency5 frequency6 ). 3%13d\n". 5%13d\n". 2%13d\n". frequency1 ). 6%13d\n". 4%13d\n". /* indicates successful termination */ 63 64 } /* end main */ Face 1 2 3 4 5 6 Frequency 1003 1017 983 994 1004 999 .53 54 55 56 57 58 59 60 61 /* display results in tabular format */ printf( "%s%13s\n". "Face".

h> Takes an integer seed and jumps to that location in its "random" sequence srand( seed )./*load <time.  srand( time( NULL ) ).More Random Number Generation  srand function   <stdlib.h> */  time( NULL )   Returns the number of seconds that have passed since January 1. 1970 ―Randomizes" the seed .

h> #include <stdio. 13 scanf( "%u".1 2 3 4 5 6 /* Fig. /* seed random number generator */ Seeds the rand 16 function 17 /* loop 10 times */ 18 for ( i = 1. &seed ). /* counter */ 10 unsigned seed. i <= 10. 5. i++ ) { 19 . /* number used to seed random number generator */ 11 12 printf( "Enter seed: " ).c Randomizing die-rolling program */ #include <stdlib. /* note %u for unsigned */ 14 15 srand( seed ).9: fig05_09.h> /* function main begins program execution */ 7 int main( void ) 8 { 9 int i.

} /* end if */ } /* end for */ 30 return 0. /* indicates successful termination */ 31 32 } /* end main */ Enter seed: 67 6 1 1 6 4 1 6 6 2 4 Enter seed: 867 2 1 4 1 6 3 1 6 6 2 Enter seed: 67 6 1 1 6 4 1 6 6 2 4 .20 21 22 23 24 25 26 27 28 29 /* pick a random number from 1 to 6 and output it */ printf( "%10d". /* if counter is divisible by 5. begin a new line of output */ if ( i % 5 == 0 ) { printf( "\n" ). 1 + ( rand() % 6 ) ).

Common Programming Error  Using srand in place of rand to generate random numbers. .

6. 10 .value becomes player's "point"  Player must roll his point before rolling 7 to win . player loses 4. 8. 9. or 12 on first throw. 3.Example: A Game of Chance   Craps simulator Rules  Roll two dice    7 or 11 on first throw. player wins 2. 5.

or LOST */ /* randomize random number generator using current time */ srand( time( NULL ) ). 5. 10 int rollDice( void ). /* can contain CONTINUE.h> /* contains prototype for function time */ /* enumeration constants represent game status */ enum Status { CONTINUE.10: fig05_10. WON.1 2 3 4 5 6 7 8 9 11 /* Fig.h> #include <stdlib. /* first roll of the dice */ /* determine game status based on sum of dice */ switch( sum ) { int myPoint.h> #include <time. /* function prototype */ 12 /* function main begins program execution */ 13 int main( void ) 14 { 15 int sum. WON and LOST /* sum of rolled dice */ /* point earned */ enum Status gameStatus. LOST }.c Craps */ #include <stdio. 16 17 18 19 20 21 22 23 24 25 26 27 sum = rollDice(). WON. . enum (enumeration) assigns numerical values to CONTINUE.

myPoint = sum.28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 /* win on first roll */ case 7: case 11: gameStatus = WON. break. /* lose on first roll */ case 2: case 3: case 12: gameStatus = LOST. /* remember point */ default: gameStatus = CONTINUE. myPoint ). /* optional */ } /* end switch */ . printf( "Point is %d\n". break. break.

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 /* while game not complete */ while ( gameStatus == CONTINUE ) { sum = rollDice(). /* indicates successful termination */ 77 } /* end main */ . } /* end else */ return 0. /* game over. } /* end if */ else { /* player lost */ printf( "Player loses\n" ). player lost */ } /* end if */ } /* end else */ } /* end while */ /* display won or lost message */ if ( gameStatus == WON ) { /* did player win? */ printf( "Player wins\n" ). /* roll dice again */ /* determine game status */ if ( sum == myPoint ) { /* win by making point */ gameStatus = WON. player won */ } /* end if */ else { if ( sum == 7 ) { /* lose by rolling 7 */ gameStatus = LOST. /* game over.

die2. return workSum. /* pick random die2 value */ workSum = die1 + die2. /* return sum of dice */ die1 = 1 + ( rand() % 6 ). /* sum of dice */ 95 } /* end function rollRice */ . die1. /* first die */ /* second die */ int workSum.78 79 /* roll dice. workSum ). int die2. /* pick random die1 value */ die2 = 1 + ( rand() % 6 ). calculate sum and display results */ 80 int rollDice( void ) 81 { 82 83 84 85 86 87 88 89 90 91 92 93 94 /* display results of this roll */ printf( "Player rolled %d + %d = %d\n". /* sum die1 and die2 */ int die1.

Player rolled 5 + 6 = 11 Player wins Player rolled Point is 5 Player rolled Player rolled Player rolled Player wins 4 + 1 = 5 6 + 2 = 8 2 + 1 = 3 3 + 2 = 5 Player rolled 1 + 1 = 2 Player loses Player rolled 6 + 4 = 10 Point is 10 Player rolled 3 + 4 = 7 Player loses .

Common Programming Error  Assigning a value to an enumeration constant after it has been defined is a syntax error. .

Good Programming Practice  Use only uppercase letters in the names of enumeration constants to make these constants stand out in a program and to indicate that enumeration constants are not variables. .

 register: tries to put variable into high-speed registers  Can only be used for automatic variables  register int counter = 1.Storage Classes  Storage class specifiers    Storage duration – how long an object exists in memory Scope – where object can be referenced in program Linkage – specifies the files in which an identifier is known (more in Chapter 14) Object created and destroyed within its block auto: default for local variables   Automatic storage   auto double x. y. .

Performance Tip  Automatic storage is a means of conserving memory. They are created when the function in which they are defined is entered and they are destroyed when the function is exited. because automatic variables exist only when they are needed. .

Why have variables stored in memory and accessible when in fact they are not needed? .Software Engineering Observation  Automatic storage is an example of the principle of least privilege—allowing access to data only when it is absolutely needed.

. register declarations are unnecessary. If intensely used variables such as counters or totals can be maintained in hardware registers. Today‘s optimizing compilers are capable of recognizing frequently used variables and can decide to place them in registers without the need for a register declaration.Performance Tips   The storage-class specifier register can be placed before an automatic variable declaration to suggest that the compiler maintain the variable in one of the computer‘s high-speed hardware registers. Often. the overhead of repeatedly loading the variables from memory into the registers and storing the results back into memory can be eliminated.

Storage Classes  Static storage    Variables exist for entire program execution Default value of zero static: local variables defined in functions.   Keep value after function ends Only known in their own function  extern:  default for global variables and functions Known in any function .

Variables used only in a particular function should be defined as local variables in that function rather than as external variables. .Software Engineering Observations   Defining a variable as global rather than local allows unintended side effects to occur when a function that does not need access to the variable accidentally or maliciously modifies it. use of global variables should be avoided except in certain situations with unique performance requirements (as discussed in Chapter 14). In general.

known in all functions Used for global variables.) . case: . function prototypes  Function scope   Can only be referenced inside a function body Used only for labels (start:. etc.Scope Rules  File scope   Identifier defined outside function. function definitions.

function parameters (local variables of function) Outer blocks "hidden" from inner blocks if there is a variable with the same name in the inner block Used for identifiers in parameter list  Function prototype scope  . ends at right brace Used for variables.Scope Rules  Block scope    Identifier declared inside a block  Block scope begins at definition.

works its way up and solves whole problem .Recursion  Recursive functions  Functions that call themselves  Can only solve a base case  Divide a problem up into  What it can do  What it cannot do  What it cannot do resembles original problem  The function launches a new copy of itself (recursion step) to solve what it cannot do  Eventually base case gets solved  Gets plugged in.

. 3! = 3 * 2! = 3 * 2 = 6..   Can compute factorials recursively Solve base case (1! = 0! = 1) then plug in   2! = 2 * 1! = 2 * 1 = 2.Recursion  Example: factorials  5! = 5 * 4 * 3 * 2 * 1  Notice that   5! = 5 * 4! 4! = 4 * 3! . .

.Recursive evaluation of 5!.

factorial( i ) ). i++ ) { printf( "%2d! = %ld\n". i <= 10. calculate factorial( i ) and display result */ for ( i = 0. 5. /* indicates successful termination */ 19 20 } /* end main */ 21 . /* function prototype */ /* function main begins program execution */ int main( void ) { int i. /* counter */ /* loop 11 times.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 /* Fig. i. } /* end for */ 18 return 0.h> long factorial( long number ). during each iteration.c Recursive factorial function */ #include <stdio.14: fig05_14.

1 ) ).22 /* recursive definition of function factorial */ 23 long factorial( long number ) 24 { 25 26 27 28 29 30 /* base case */ if ( number <= 1 ) { return 1. } /* end if */ else { /* recursive step */ return ( number * factorial( number . 31 } /* end else */ 32 33 } /* end function factorial */ 0! 1! 2! 3! 4! 5! 6! 7! 8! 9! 10! = = = = = = = = = = = 1 1 2 6 24 120 720 5040 40320 362880 3628800 .

eventually exhausting memory. . This is analogous to the problem of an infinite loop in an iterative (nonrecursive) solution. Infinite recursion can also be caused by providing an unexpected input. will cause infinite recursion.Common Programming Error  Either omitting the base case. or writing the recursion step incorrectly so that it does not converge on the base case.

1) + fibonacci( n – 2 ).. 5. 2. 1. } . else return fibonacci( n . 8.   Each number is the sum of the previous two Can be solved recursively:  fib( n ) = fib( n .Example Using Recursion: Fibonacci Series  Fibonacci series: 0.1 ) + fib( n – 2 )  Code for the fibonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n.. 3. 1.

h> long fibonacci( long n ). 5. /* indicates successful termination */ 24 25 } /* end main */ 26 . /* calculate fibonacci value for number input by user */ result = fibonacci( number ). /* display result */ printf( "Fibonacci( %ld ) = %ld\n". &number ). 22 23 return 0.c Recursive fibonacci function */ #include <stdio. scanf( "%ld". /* fibonacci value */ long number. number. result ).1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 /* Fig. /* function prototype */ /* function main begins program execution */ int main( void ) { long result. /* number input by user */ /* obtain integer from user */ printf( "Enter an integer: " ).15: fig05_15.

1 ) + fibonacci( n . } /* end if */ else { /* recursive step */ return fibonacci( n .2 ). } /* end else */ 38 } /* end function fibonacci */ Enter an integer: 0 Fibonacci( 0 ) = 0 Enter an integer: 1 Fibonacci( 1 ) = 1 Enter an integer: 2 Fibonacci( 2 ) = 1 (continued on next slide… ) .27 /* Recursive definition of function fibonacci */ 28 long fibonacci( long n ) 29 { 30 31 32 33 34 35 36 37 /* base case */ if ( n == 0 || n == 1 ) { return n.

(continued from previous slide…) Enter an integer: 3 Fibonacci( 3 ) = 2 Enter an integer: 4 Fibonacci( 4 ) = 3 Enter an integer: 5 Fibonacci( 5 ) = 5 Enter an integer: 6 Fibonacci( 6 ) = 8 (continued on next slide… ) .

(continued from previous slide…) Enter an integer: 10 Fibonacci( 10 ) = 55 Enter an integer: 20 Fibonacci( 20 ) = 6765 Enter an integer: 30 Fibonacci( 30 ) = 832040 Enter an integer: 35 Fibonacci( 35 ) = 9227465 .

Set of recursive calls for fibonacci(3). .

Iteration  Repetition   Iteration: explicit loop Recursion: repeated function calls  Termination   Iteration: loop condition fails Recursion: base case recognized   Both can have infinite loops Balance  Choice between performance (iteration) and good software engineering (recursion) .Recursion vs.

Another reason to choose a recursive solution is that an iterative solution may not be apparent. . Avoid using recursion in performance situations. A recursive approach is normally chosen in preference to an iterative approach when the recursive approach more naturally mirrors the problem and results in a program that is easier to understand and debug.Tips & Observations   Any problem that can be solved recursively can also be solved iteratively (nonrecursively). Recursive calls take time and consume additional memory.

debug. they are more difficult to program..Performance Tip  Functionalizing programs in a neat.e. A heavily functionalized program—as compared to a monolithic (i. maintain. and these consume execution time on a computer‘s processor(s). and evolve. hierarchical manner promotes good software engineering. one-piece) program without functions—makes potentially large numbers of function calls. But it has a price. . test. So. although monolithic programs may perform better.

multiplies these two numbers.Exercises   Write a void function named product( ) that computes the product of three integers passed to it and displays the result. and returns the result. Write a function named mult( ) that accepts two floating point numbers as parameters. .

 use a switch selection statement to use the appropriate calculation for the recommended calories for the selected lifestyle. Multiply the activity factor by the person's weight to get the recommended number of calories. Base your recommendation on the person's weight and whether the person has an active or sedentary (inactive) lifestyle. If the person is active.  print out your results on the screen. that person's activity factor is 13.  having the user enter whether they have active or sedentary lifestyle. 'A' for active or 'S' sedentary.Lab Assignment #2 Calculation of Calories Write a program that recommends the number of calories a person should eat each day. . as a floating point number. as a character. that person's activity factor is 15. Calories are units of energy found in all foods. Start your program by:  having the user enter their weight. If the person is sedentary.

SEE YOU NEXT LESSON!  .