functions recall n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Functions-Recall PowerPoint Presentation
Download Presentation
Functions-Recall

Loading in 2 Seconds...

play fullscreen
1 / 42

Functions-Recall - PowerPoint PPT Presentation


  • 60 Views
  • Uploaded on

Functions-Recall. Parameter passing & return. void main() { int x=10, y=5; printf (“M1: x = %d, y = %dn”, x, y); interchange (x, y); printf (“M2: x = %d, y = %dn”, x, y); } void interchange (int x, int y) { int temp; printf (“F1: x = %d, y = %dn”, x, y);

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

Functions-Recall


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
parameter passing return
Parameter passing & return

void main()

{

int x=10, y=5;

printf (“M1: x = %d, y = %d\n”, x, y);

interchange (x, y);

printf (“M2: x = %d, y = %d\n”, x, y);

}

void interchange (int x, int y)

{ int temp;

printf (“F1: x = %d, y = %d\n”, x, y);

temp= x; x = y; y = temp;

printf (“F2: x = %d, y = %d\n”, x, y);

}

Output

parameter passing return1
Parameter passing & return

void main()

{

int x=10, y=5;

printf (“M1: x = %d, y = %d\n”, x, y);

interchange (x, y);

printf (“M2: x = %d, y = %d\n”, x, y);

}

void interchange (int x, int y)

{ int temp;

printf (“F1: x = %d, y = %d\n”, x, y);

temp= x; x = y; y = temp;

printf (“F2: x = %d, y = %d\n”, x, y);

}

Output

M1: x = 10, y = 5

F1: x = 10, y = 5

F2: x = 5, y = 10

M2: x = 10, y = 5

parameter passing return2
Parameter passing & return

int x=11,y=6;

void interchange (int a, int b);

int main()

{

{ int x=6,y=11;

interchange (x,y);

}

printf("x=%d y=%d",x,y);

}

void interchange (int x, int y)

{ int temp;

temp=x;

x=y;

y=temp;

}

Homework

how are function calls implemented
How are function calls implemented?
  • The following applies in general, the implementation details of function call
    • The system maintains a stack in memory
      • Stack is a last-in first-out structure
      • Two operations on stack, push and pop
    • Whenever there is a function call, the activation record gets pushed into the stack
      • Activation record consists of the return address in the calling program, the return value from the function, and the local variables inside the function
slide6

Local

Variables

Return Value

Return Addr

  • Pop activation record, whenever the function returns
  • Activation record looks like:
slide7

void main()

{

……..

x = gcd (a, b);

……..

}

int gcd (int x, int y)

{

……..

……..

return (result);

}

Local

Variables

Activation record

STACK

Return Value

Return Addr

After return

Before call

After call

slide8

void main()

{

……..

x = ncr (a, b);

……..

}

int ncr (int n, int r)

{

x=fact(n); y=fact(r); z=fact(n-r);

p=x/(y*z)

return (p);

}

int fact (int n)

{

………

return (result);

}

3 times

3 times

LV2, RV2, RA2

LV1, RV1, RA1

LV1, RV1, RA1

LV1, RV1, RA1

Before call

Call ncr

Call fact

fact returns

ncr returns

push activation record
Push activation record

a, b (Local x

Variables)

main

Return Value

Return Addr

push activation record1
Push activation record

Parameter passing

n, r, p (Local

Variables)

nCr

Return Value

Return Addr

a, b (Local x

Variables)

main

Return Value

Return Addr

slide11

void main()

{

……..

x = ncr (a, b);

……..

}

int ncr (int n, int r)

{

x=fact(n); y=fact(r); z=fact(n-r);

p=x/(y*z)

return (p);

}

int fact (int n)

{

………

return (result);

}

3 times

3 times

LV2, RV2, RA2

LV1, RV1, RA1

LV1, RV1, RA1

LV1, RV1, RA1

Before call

Call ncr

Call fact

fact returns

ncr returns

push activation record2
Push activation record

n, result (Local

Variables)

fact

result

Return Value

Return

Return Addr

Parameter passing

x

n, r (Local

Variables)

nCr

Return Value

Return Addr

a, b (Local x

Variables)

main

Return Value

Return Addr

pop activation record
Pop activation record

Local

Variables

nCr

Return Value

Return Addr

Local

Variables

Return Value

main

Return Addr

slide14

void main()

{

……..

x = ncr (a, b);

……..

}

int ncr (int n, int r)

{

x=fact(n); y=fact(r); z=fact(n-r);

p=x/(y*z)

return (p);

}

int fact (int n)

{

………

return (result);

}

3 times

3 times

LV2, RV2, RA2

LV1, RV1, RA1

LV1, RV1, RA1

LV1, RV1, RA1

Before call

Call ncr

Call fact

fact returns

ncr returns

push activation record3
Push activation record

Local

Variables

fact

result

Return Value

Return Addr

y

Local

Variables

nCr

Return Value

Return Addr

Local

Variables

Return Value

main

Return Addr

pop activation record1
Pop activation record

Local

Variables

nCr

Return Value p

Return

Return Addr

Local x

Variables

Return Value

main

Return Addr

pop activation record2
Pop activation record

a, b (Local x

Variables)

main

Return Value

Return Addr

recursion1
Recursion
  • A process by which a function calls itself repeatedly
    • Either directly.
      • X calls X
    • Or cyclically in a chain.
      • X calls Y, and Y calls X
  • Used for repetitive computations in which each action is stated in terms of a previous result

fact(n) = n * fact (n-1)

contd
Contd.
  • For a problem to be written in recursive form, two conditions are to be satisfied:
    • It should be possible to express the problem in recursive form
      • Solution of the problem in terms of solution of the same problem on smaller sized data
    • The problem statement must include a stopping condition

fact(n) = 1, if n = 0

= n * fact(n-1), if n > 0

Stopping condition

Recursive definition

slide21
Examples:
    • Factorial:

fact(0) = 1

fact(n) = n * fact(n-1), if n > 0

    • Fibonacci series (1,1,2,3,5,8,13,21,….)

fib (0) = 1

fib (1) = 1

fib (n) = fib (n-1) + fib (n-2), if n > 1

factorial
Factorial

long int fact (int n)

{

if (n == 1)

return (1);

else

return (n * fact(n-1));

}

factorial execution
Factorial Execution

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

factorial execution1

fact(4)

Factorial Execution

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

factorial execution2

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

Factorial Execution

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

factorial execution3

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

Factorial Execution

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

factorial execution4

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

if (2 = = 1) return (1);

else return (2 * fact(1));

Factorial Execution

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

factorial execution5

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

if (2 = = 1) return (1);

else return (2 * fact(1));

Factorial Execution

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

if (1 = = 1) return (1);

factorial execution6

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

if (2 = = 1) return (1);

else return (2 * fact(1));

1

Factorial Execution

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

if (1 = = 1) return (1);

factorial execution7

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

if (2 = = 1) return (1);

else return (2 * fact(1));

1

Factorial Execution

2

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

if (1 = = 1) return (1);

factorial execution8

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

if (2 = = 1) return (1);

else return (2 * fact(1));

1

Factorial Execution

2

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

if (1 = = 1) return (1);

factorial execution9

fact(4)

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

if (2 = = 1) return (1);

else return (2 * fact(1));

1

Factorial Execution

6

2

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

if (1 = = 1) return (1);

factorial execution10

fact(4)

24

if (4 = = 1) return (1);

else return (4 * fact(3));

if (3 = = 1) return (1);

else return (3 * fact(2));

if (2 = = 1) return (1);

else return (2 * fact(1));

1

Factorial Execution

6

2

long int fact (int n)

{

if (n = = 1) return (1);

else return (n * fact(n-1));

}

if (1 = = 1) return (1);

what happens for recursive calls
What happens for recursive calls?
  • What we have seen ….
    • Activation record gets pushed into the stack when a function call is made
    • Activation record is popped off the stack when the function returns
  • In recursion, a function calls itself
    • Several function calls going on, with none of the function calls returning back
      • Activation records are pushed onto the stack continuously
      • Large stack space required
slide35

Local

Variables

Return Value

Return Addr

    • Activation records keep popping off, when the termination condition of recursion is reached
  • We shall illustrate the process by an example of computing factorial
    • Activation record looks like:
example main calls fact 3
Example:: main() calls fact(3)

void main()

{

int n;

n = 3;

printf (“%d \n”, fact(n) );

}

int fact (n)

int n;

{

if (n = = 0)

return (1);

else

return (n * fact(n-1));

}

slide37

n = 0

1

RA .. fact

n = 1

n = 1

n = 1

main calls fact

-

1*1 = 1

-

RA .. fact

RA .. fact

RA .. fact

n = 2

n = 2

n = 2

n = 2

n = 2

-

-

-

2*1 = 2

-

RA .. fact

RA .. fact

RA .. fact

RA .. fact

RA .. fact

n = 3

n = 3

n = 3

n = 3

n = 3

n = 3

n = 3

-

-

-

-

3*2 = 6

-

-

RA .. main

RA .. main

RA .. main

RA .. main

RA .. main

RA .. main

RA .. main

TRACE OF THE STACK DURING EXECUTION

fact returns to main

look at the variable addresses a slightly different program
Look at the variable addresses (a slightly different program) !

void main()

{

int x,y;

scanf("%d",&x);

y = fact(x);

printf ("M: x= %d, y = %d\n", x,y);

}

int fact(int data)

{ int val = 1;

printf("F: data = %d, &data = %u \n

&val = %u\n", data, &data, &val);

if (data>1) val = data*fact(data-1);

return val;

}

Output

4

F: data = 4, &data = 3221224528

&val = 3221224516

F: data = 3, &data = 3221224480

&val = 3221224468

F: data = 2, &data = 3221224432

&val = 3221224420

F: data = 1, &data = 3221224384

&val = 3221224372

M: x= 4, y = 24

fibonacci numbers
Fibonacci Numbers

Fibonacci recurrence:

fib(n) = 1 if n = 0 or 1;

= fib(n – 2) + fib(n – 1)

otherwise;

int fib (int n) {

if (n == 0 or n == 1)

return 1; [BASE]

return fib(n-2) + fib(n-1) ;

[Recursive]

}

slide40

int fib (int n) {

if (n == 0 || n == 1)

return 1;

return fib(n-2) + fib(n-1) ;

}

Fibonacci recurrence:

fib(n) = 1 if n = 0 or 1;

= fib(n – 2) + fib(n – 1)

otherwise;

fib (5)

fib (3)

fib (4)

fib (1)

fib (2)

fib (2)

fib (3)

fib (0)

fib (1)

fib (1)

fib (2)

fib (0)

fib (1)

fib (1)

fib (0)

slide41

int fib (int n) {

if (n == 0 || n == 1)

return 1;

return fib(n-2) + fib(n-1) ;

}

Fibonacci recurrence:

fib(n) = 1 if n = 0 or 1;

= fib(n – 2) + fib(n – 1)

otherwise;

fib (5)

fib (3)

fib (4)

fib (1)

fib (2)

fib (2)

fib (3)

1

fib (0)

fib (1)

fib (1)

fib (2)

fib (0)

fib (1)

1

1

1

1

1

fib (1)

fib (0)

1

1

fib.c

slide42

int fib (int n) {

if (n==0 || n==1)

return 1;

return fib(n-2) + fib(n-1) ;

}

Fibonacci recurrence:

fib(n) = 1 if n = 0 or 1;

= fib(n – 2) + fib(n – 1)

otherwise;

8

fib (5)

3

5

fib (3)

fib (4)

2

3

1

2

fib (1)

fib (2)

fib (2)

fib (3)

1

2

1

1

1

1

1

fib (0)

fib (1)

fib (1)

fib (2)

fib (0)

fib (1)

1

1

1

1

1

1

1

fib (1)

fib (0)

1

1