senem kumova metin // cs115 // 2008-20091 functions continues chapter 5

20
Senem Kumova Metin // CS115 // 2 008-2009 1 FUNCTIONS continues CHAPTER 5

Upload: rodney-horton

Post on 31-Dec-2015

220 views

Category:

Documents


1 download

TRANSCRIPT

Senem Kumova Metin // CS115 // 2008-2009 1

FUNCTIONS continues

CHAPTER 5

Senem Kumova Metin // CS115 // 2008-2009 2

1. CALL BY VALUE

2. DEVELOPING A LARGE PROGRAM

3. SCOPE RULES

4. STORAGE CLASSES

5. RECURSION

Senem Kumova Metin // CS115 // 2008-2009 3

CALL BY VALUE

• If a variable is passed to a function, the stored value in the calling environment will not be changed!!!

EXAMPLE:#include<stdio.h>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 valueprintf(“%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 // 2008-2009 4

Developing a large programread 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<stdio.h>#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 // 2008-2009 5

SCOPE RULES

• Each identifier is accessible only within the block in which it has been declared!!!

• EXAMPLE: #include<stdio.h>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

a b c

a b d c

func2() variables

a b cd

main() variables

Senem Kumova Metin // CS115 // 2008-2009 6

SCOPE RULES : EXAMPLE

# include <stdio.h>

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:258.20000093

Senem Kumova Metin // CS115 // 2008-2009 7

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. register4. 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 // 2008-2009 8

STORAGE CLASSES2. 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 // 2008-2009 9

STORAGE CLASSES2. 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 <stdio.h>#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: 1 2 3 12 4 2 3

Senem Kumova Metin // CS115 // 2008-2009 10

STORAGE CLASSES3. 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 // 2008-2009 11

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<stdio.h>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 // 2008-2009 12

STORAGE CLASSESSTATIC 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<stdio.h>

void f(void){...... } // v is not available

static int v; // static external variable

void g(void){ ..... } // you can use v

Senem Kumova Metin // CS115 // 2008-2009 13

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 // 2008-2009 14

RECURSION : EXAMPLES

void main(void){ printf(“I am calling myself!\n”);

main(); }

/* NO BASE CASE SO IT WILL NEVER STOP */

#include<stdio.h>void rev( ){ char ch;

ch=getchar(); if (ch != '\n') rev(); putchar(ch); } // BASE CASE ch=‘\n’

int main( ){ rev(); }

Senem Kumova Metin // CS115 // 2008-2009 15

RECURSION : EXAMPLES

// ITERATIONint sum(int N) { int i;

int SUM =0;

for (i=1; i<=N; i++)

SUM=SUM+1;

return SUM; }

// RECURSIONint sum(int N){ if(N==1) return 1;

else return(N+sum(N-1)); }

/* BASE CASE : N==1 1METHOD : sum(N) = N + sum(N-1)

if BASE CASEelse METHOD

*/

)()1(.......43211

NsumiNNNi

i

Senem Kumova Metin // CS115 // 2008-2009 16

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); }

)(11

1

1.......

4

1

3

1

2

1

1

1

1

NsumiNN

Ni

i

Senem Kumova Metin // CS115 // 2008-2009 17

RECURSION & ITERATION EXAMPLE : FACTORIAL

/* iterative versionn! = 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 1METHOD : 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 // 2008-2009 18

RECURSION & ITERATION EXAMPLE : POWER

/* iterative versionxn = 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 xn = xn-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 1METHOD : 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 // 2008-2009 19

RECURSION EXAMPLE : FIBONACCI

In mathematics, the Fibonacci numbers form a sequence defined recursively 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 1METHOD : fibonacci(n)=fibonacci(n-1)+ fibonacci(n-2)*/

Senem Kumova Metin // CS115 // 2008-2009 20

YOUR HOMEWORK

• TOWERS OF HANOI