lec6 - CS240: Programming in C Lecture 6: Recursive...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS240: Programming in C Lecture 6: Recursive Functions C Pre-processor. 1 Tuesday, February 1, 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Functions: extern and static Functions can be used before they are declared static for a function means the function is local only to that file extern, means that the function was declared in another file or the same file but later Always put prototype before definition to avoid any problems 2 Tuesday, February 1, 2011
Background image of page 2
Variables All variables must be declared before use extern has the same meaning as for functions static the same when declared outside functions static declared within a function ‘has memory’, i.e is initialized only the first time the function is called Don’t use the same names for global and local variables 3 Tuesday, February 1, 2011
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Passing Parameters In C, parameters are passed to functions BY VALUE Functions create local copies of those variables Modifications are not preserved outside the functions unless the function is passed references to variables int swap(int[]) 4 Tuesday, February 1, 2011
Background image of page 4
Recursive functions in C A function can call itself Recursive expression of the function Needs a stop condition Example: compute n! i n t f a c t ( n ) { i f ( n < = 1 ) r e t u r n 1 ; else r e t u r n n * f a c t ( n - 1 ) ; } Why does it work? Typically used to compute an inductively defined property 5 Tuesday, February 1, 2011
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Induction Suppose that P is a predicate on natural numbers. Suppose P(0) holds And, for all i, P(i) => P(i+1) Then, P(n) holds for all n. Let P be the “factorial property”: P(0) = 1 P(n) = n * P(n-1) If we know P(n) then we have an algorithm to compute P(n+1) simply multiply n * P(n) 6 Tuesday, February 1, 2011
Background image of page 6
Operationally… it’s all about the Stack The operating system creates a process by assigning memory and other resources Stack : keeps track of the point to which each active subroutine should return control when it Fnishes executing; stores variables that are local to functions Heap : dynamic memory for variables that are created with malloc, calloc, realloc and disposed of with free Data : initialized variables including global and static
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 32

lec6 - CS240: Programming in C Lecture 6: Recursive...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online