C Programming for Engineers Functions & Recursions ICEN 360– Spring 2017 Prof. Dola Saha 1
Classwork Assignment Ø Compute rim area of a flat washer using function to calculate the area. 2
Writing a function to calculate area 3
Math Library Functions Ø Performs common mathematical calculations. 4
More Math Library Functions Ø #include <math.h> 5
Self Review Assignment Ø Modify the program to compute the rim area of a flat washer using two functions § To calculate the area § To compute the square 6
Function call stack and stack frames Ø Stack is analogous to a pile of books Ø Known as last-in, first-out (LIFO) data structures Pop Push Stack of books 7
Function call stack Ø Supports function call & return Ø Supports creation, maintenance & destruction of each called function’s local variables Ø Keeps track of return addresses that each function needs to return control to the caller function Ø Function call à an entry is pushed to stack Ø Function return à an entry is popped from stack 8
Example C Code 9
Function call & Stack frame 10
Function call & Stack frame 11
Function call & Stack frame 12
Passing argument by value & by pointer Pass by Value Pass by Pointer A copy of argument’s value is made and passed An address to the argument is passed to the to the function function Changes to copy do not change the original Changes to the value of the address does value change the original value Most commonly used Should be used by trusted functions only 13
Example Pass-by-value & Pass-by-reference Output 14
Recursion Ø A recursive function is a function that calls itself either directly or indirectly through another function. Ø Nature of recursion § One or more simple cases of the problem have a straightforward, nonrecursive solution. § The other cases can be redefined in terms of problems that are closer to the simple cases. 15
Recursively calculating Factorial Ø The factorial of a nonnegative integer n , written n! (pronounced “ n factorial”), is the product n · ( n –1) · ( n – 2) · … · 1 o with 1! equal to 1, and 0! defined to be 1. Ø A recursive definition of the factorial function is arrived at by observing the following relationship: n! = n · (n – 1)! Ø Proof: n! = n · (n-1) · (n-2) ·…… · 2 · 1 n! = n · ( (n-1) · (n-2) ·…… · 2 · 1) n! = n · ((n-1)!) 16
Recursive evaluation of 5! 17
Recursive Factorial C Code (1) 18
Recursive Factorial C Code (2) 19
Recursive Factorial C Code (3) – Output 20
Example Fibonacci Series by Recursion Ø The Fibonacci series o 0, 1, 1, 2, 3, 5, 8, 13, 21, … The Fibonacci series may be defined recursively as follows: Ø fibonacci(0) = 0 fibonacci(1) = 1 fibonacci( n ) = fibonacci(n – 1) + fibonacci(n – 2) 21
Recursive Fibonacci Series C Code (1) 22
Recursive Fibonacci Series C Code (2) 23
Recursive calls 24
Recursion vs Iteration Ø Both iteration and recursion are based on a control statement: Iteration uses a repetition statement; recursion uses a selection statement . Ø Both iteration and recursion involve repetition: Iteration explicitly uses a repetition statement; recursion achieves repetition through repeated function calls . Ø Iteration and recursion each involve a termination test : Iteration terminates when the loop-continuation condition fails ; recursion when a base case is recognized . 25
Recursion is expensive Ø It repeatedly invokes the mechanism, and consequently the overhead, of function calls . Ø This can be expensive in both processor time and memory space. Ø Each recursive call causes another copy of the function to be created; this can consume considerable memory . Ø The amount of memory in a computer is finite, so only a certain amount of memory can be used to store stack frames on the function call stack. Ø If more function calls occur than can have their stack frames stored on the function call stack, a fatal error known as a stack overflow occurs. 26
Classwork Assignment Ø Write a C Program to find product of 2 Numbers using Recursion Ø Example: § Multiply 6 by 3 Ø Generalization: § Divide it into two problems: Multiply 6 by 2 1. § If n is 1, Add 6 to the result of problem 1 2. o ans is m. § Split problem 1 into 2 smaller problems: § Else Multiply 6 by 2 1. o ans is m + multiply(m-1) Multiply 6 by 1 a) Add 6 to the result of problem 1a) b) Add 6 to the result of problem 1 2. 27
Classwork Assignment Ø The greatest common divisor of integers x and y is the largest integer that evenly divides both x and y. Write a recursive function gcd that returns the greatest common divisor of x and y. Ø The gcd of x and y is defined recursively as follows: § If y is equal to 0, then gcd(x, y) is x; § otherwise gcd(x, y) is gcd(y, x % y), where % is the remainder operator. 28
Storage Class register static auto extern Ø Storage class determines storage duration, scope and linkage Ø Duration: Period when the identifier exists in memory Ø Scope: Where the identifier can be referenced in a program Ø Linkage: Determines whether the identifier is known in current file or other linked ones 29
auto Storage Class Ø Keyword auto is used to declare variables of automatic storage duration. Ø Created when the block in which they’re defined is entered Ø Exists while the block is active Ø Destroyed when the block is exited Ø Local variables – declared within function argument or body, are auto by default Ø auto keyword is rarely used 30
static Storage Class Ø Keywords extern and static are used for static storage Ø Exists from beginning of execution until end Ø Storage is allocated and initialized only once , before the program begins execution Ø Scope defines where it can be accessed from 31
32
Recommend
More recommend