Senem Kumova Metin // CS115 // FUNCTIONS continues CHAPTER 5
Senem Kumova Metin // CS115 // CALL BY VALUE 2.DEVELOPING A LARGE PROGRAM 3.SCOPE RULES 4.STORAGE CLASSES 5.RECURSION
Senem Kumova Metin // CS115 // CALL BY VALUE If a variable is passed to a function, the stored value in the calling environment will not be changed!!! EXAMPLE: #include int my_sum(int n); void main(void) {int n=9; printf(“%d\n”,n); printf(“%d\n”,my_sum(n)); // call function my_sum() by value printf(“%d\n”,n); } int my_sum(int n) { n=n+2; // stored value of n in my_sum() is changed return n; } OUTPUT ??
Senem Kumova Metin // CS115 // Developing a large program read 5.8 (pg 209) /* my_program.c */ #include”my_header.h” void main(void) { int a=3, b=4, result=0; result=my_sum(a,b); printf(“%d\n”,result); result=my_subtract(a,b); printf(“%d\n”,result); printf(“%f\n”,PI); } /* my_header.h */ #include #define PI 3.14 int my_sum(int x,int y) { x++; y++; return x+y; } int my_subtract(int x, int y) {return x-y-1; }
Senem Kumova Metin // CS115 // SCOPE RULES Each identifier is accessible only within the block in which it has been declared!!! EXAMPLE: #include void func_1 (int a) {int b, c; } void func_2 (int a, double b, float d) { char c; } void main () { int a,b,d; char c; } func1() variables abc abdc func2() variables abcd main() variables
Senem Kumova Metin // CS115 // SCOPE RULES : EXAMPLE # include void main (void) {int a=2; float b=3.2; // outer block a printf("%d\n",a); {int a =5; // inner block a printf("%d\n", a); printf("%f\n", a+b); { int b=4; printf("%d\n“, a+b); } printf("%d\n",++a); } OUTPUT:
Senem Kumova Metin // CS115 // STORAGE CLASSES EACH VARIABLE HAS A TYPE AND A STORAGE CLASS STORAGE CLASSES 1.auto /* variables declared within function bodies are automatic by default */ 2.extern /* look for it elsewhere either in this file or in some other file */ 3.register 4.static EXAMPLE extern int a=1; /* type of variable a is int, storage class of a is extern */ auto float f; float z; /* storage class is auto and data type is float */
Senem Kumova Metin // CS115 // STORAGE CLASSES 2. EXTERN The keyword extern is used to tell the compiler to "look for it elsewhere, either in this file or in some other file" External variables never disappear. Because they exist throughout the execution life of the program, they can be used to transmit values across functions ( GLOBAL)
Senem Kumova Metin // CS115 // STORAGE CLASSES 2. EXTERN /* file1.h */ int f(void) { extern int a; /* look for it elsewhere */ int b, c; // local b and c a = b = c = 4; // global b anc c are masked return (a + b + c); } /* prog1.c*/ #include #include ”file1.h” int a = 1, b = 2, c = 3; /* global variables */ int f(void); int main(void) { printf("%3d%3d%3d\n", a, b, c); printf("%3d\n", f() ); printf("%3d%3d%3d\n", a, b, c); return 0; } OUTPUT:
Senem Kumova Metin // CS115 // STORAGE CLASSES 3. REGISTER Forces compiler that these variables should be stored in high speed memory registers Compiler may or may not store them in registers Reading data from registers is faster comparing to reading from memory EXAMPLES: register char c; register int a=1; /* IS EQUIVALENT TO */ register a=1; // default data type is int
Senem Kumova Metin // CS115 // STORAGE CLASSES 4. STATIC Memory opened for the static variable will not be closed after the execution of the function Initialization for static variables is done just once in the first execution of the function EXAMPLE: #include int ver(); main() {printf(“1. value=%d\n”, ver() ); printf(“2. value=%d\n”, ver() ); printf(“3. value=%d\n”, ver() ); } int ver() {static int k =0; k=k+5; return k; } OUTPUT 1. value=5 2. value=10 3. value=15
Senem Kumova Metin // CS115 // STORAGE CLASSES STATIC EXTERNAL VARIABLES Can be seen below the prototype/declaration but can not be seen in any other file Scope restricted Cannot be used with functions defined earlier or in any other files Program security !! EXAMPLE : #include void f(void) { } // v is not available static int v; // static external variable void g(void) {..... } // you can use v
Senem Kumova Metin // CS115 // RECURSION An algorithmic technique where a function, in order to accomplish a task, calls itself with some part of the task If a function calls itself, then it is called recursive !!! There are two parts to the definition of a recursive solution: –Base case: Simple, non-recursive solution –General method: Uses simpler version of the problem to solve harder problem
Senem Kumova Metin // CS115 // RECURSION : EXAMPLES void main(void) { printf(“I am calling myself!\n”); main(); } /* NO BASE CASE SO IT WILL NEVER STOP */ #include void rev( ) { char ch; ch=getchar(); if (ch != '\n') rev(); putchar(ch);} // BASE CASE ch=‘\n’ int main( ) { rev();}
Senem Kumova Metin // CS115 // RECURSION : EXAMPLES // ITERATION int sum(int N) { int i; int SUM =0; for (i=1; i<=N; i++) SUM=SUM+1; return SUM; } // RECURSION int sum(int N) { if(N==1) return 1; else return(N+sum(N-1));} /* BASE CASE : N==1 1 METHOD : sum(N) = N + sum(N-1) if BASE CASE else METHOD */
Senem Kumova Metin // CS115 // RECURSION : EXAMPLES BASE CASE N=1 return 1 METHOD sum(N)= sum(N-1)+1/N double sum(int N) {if(N==1) return 1; else return (sum(N-1)+1/N); }
Senem Kumova Metin // CS115 // RECURSION & ITERATION EXAMPLE : FACTORIAL /*iterative version n! = 1*2*3* … * n = n*(n-1)*(n-2) * …*3*2*1 */ int factorial (int n) { int product=1; for(;n>1;--n) product=product*n; return product;} /* recursive version n! = n * (n-1)! */ int factorial(int n) { if(n<=1) return 1; else return ( n*factorial(n-1) ); } /* BASE CASE : n<=1 1 METHOD : n!=n*(n-1)! */ factorial(3) = 3 * factorial(2) = 3 * ( 2 * factorial(1) ) = 3 * ( 2 * ( 1 * factorial(0) )) = 3 * ( 2 * ( 1 * 1 ) )) = 6
Senem Kumova Metin // CS115 // RECURSION & ITERATION EXAMPLE : POWER /*iterative version x n = x*x*x* … *x */ /* Assume that n is always positive*/ int power (int x, int n) { int result=1; int i; for(i=1; i<=abs(n); i++) result=result*x; } /* recursive version x n = x n-1 * x */ /* Assume that n is always positive */ int power(int x, int n) { if(n==1) return x; else return ( x* power(x,n-1) ); } /* BASE : n==1 1 METHOD : power(x,n)= power(x,n-1)*x */ power(3,4) = 3 * power(3,3) = 3 * ( 3 * power(3,2) ) = 3 * ( 3 * ( 3 * power(3,1) )) = 3 * ( 3 * ( 3 * 3 ) ) = 81
Senem Kumova Metin // CS115 // RECURSION EXAMPLE : FIBONACCI In mathematics, the Fibonacci numbers form a sequence definedmathematicssequence recursivelyrecursively by for n=0 fibonacci(0) = 0 fibonacci (n) = for n=1 fibonacci(1) = 1 for n >1 fibonacci(n) = fibonacci(n-1) + fibonacci(n-2) int fibonacci(int n) { if(n<=1) return 1; else return ( fibonacci(n-1)+fibonacci(n-2) );} /* BASE CASE : n<=1 1 METHOD : fibonacci(n)=fibonacci(n-1)+ fibonacci(n-2)*/
Senem Kumova Metin // CS115 // YOUR HOMEWORK TOWERS OF HANOI