CCP503

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

DEPARTMENT OF TECHNICAL EDUCATION

ANDHRAPRADESH
Name : K.Sreeramacharyulu
Designation : Lecturer
Branch : Commercial and Computer Practice
Institute : Govt Polytechnic, Srikakulam
Year/Semester : V Semester
Subject : VISUAL BASIC-I
Sub. Code : CCP-503
Topic : Programming Fundamentals Know the
structure of VB applications
Duration : 50 Minutes
Sub. Topic : Know the Recursion concept in VB
Teaching Aids : PPT, Clips and Images
CCP503.39 1
Objectives

On completion of this period you would be able


to Know…

 About the concepts of Recursion in VB

 Different applications of Recursive Functions in VB

 The behavior of Recursive Programs in VB

CCP503.39 2
RECAP

In the previous session you have learnt ….about

4. Built-in functions

CCP503.39 3
THE RECURSION IN VB

 Definition : A recursive procedure is defined as that


the procedure, which calls, itself again and again.

 In order to implement Recursion in any programming


Language, the STACK Data Structure should be
supported by the language. Otherwise it is not possible.

 Recursive programming is used for implementing


algorithms, or mathematical definitions, or Fibonacci
Number series like 1,1, 2, 3, 5, 8, 13, 21, 34 etc,.

CCP503.39 4
A simple Example : Write a program in VB to accept
a number and then compute its factorial by using
Recursion concept.

 In the calculation of the factorial of a number with


an N (N!) exclamation mark, is descried
recursively as follows

N! = N * (N - 1)!

CCP503.39 5
 The factorial of N is the number N multiplied by the
factorial of (N - 1), which in turn multiplied by factorial
of (N – 1), which in turn is ( N – 1) multiplied by
factorial of (N – 2) and so on, until we reach 0!, which
is ‘1’

CCP503.39 6
 Here’s the process of calculation of the factorial of
Number 5 : 5 != 120

 =5 * 4 =5* fact (5-1)


= 5 * 4 * 3! =5*4*fact(4-1)
= 5 * 4 * 3 * 2! =5*4*3*fact(3-1)
= 5 * 4 * 3 * 2 * 1! =5*4*3*2*fact(2-1)
= 5 * 4 * 3 * 2 * 1 * 0! =5*4*3*2*1*fact(1-1)
=5*4*3*2*1*1 =5*4*3*2*1*fact(1-1)
= 120 =5 * 4 * 3 * 2 * 1 * 1= 120

CCP503.39 7
 For the mathematically inclined, the factorial of
the number N is defined as follows:

 N! = N * (N – 1)! If N is greater than zero

 N! = 1 If N is zero

CCP503.39 8
Program to compute Factorial Using
Recursion

Follow the steps to develop application: As in Fig 39.1

 Open a new Standard EXE project & name it as FrmFact

 Add one command buttons then make its caption as


”Click here to calculate Factorial”

 Add two Text Boxes and Two Label Boxes

 Name the text boxes as TxtNum & TxtFact respectively


 Name the Label boxes as LblNum & LblFact respectively

CCP503.39 9
Enter code for ”Click here to calculate Factorial”
button and define a function factorial respectively
 Now run application
 Enter an integer value in text1 & click calculate
button
 Now the Factorial value of integer value will be
displayed in Text2 Box named as Txtfact as in Fig
39.1
 The code window containing real recursion program
in fig 39.2 as given below.

CCP503.39 10
Fig 39.2 CCP503.39 11
Fig 39.1 CCP503.39 12
Program to display Fibonacci series
using Recursion

Follow the steps to develop application: As in Fig 39.3

 Open a new Standard EXE project & name it as Form1

 Add Command buttons & name them as ”calculate” ,exit

 Add one text and label boxes

CCP503.39 13
 Enter code for calculate button and define a
function Fibonacci respectively

 Now run application

 Enter an integer value in text1 & click calculate


button

 Now the Fibonacci sum of integer value will be


displayed in screen itself , as in Fig 39.3

CCP503.39 14
Fig 39.3 CCP503.39 15
ADVANTAGES OF RECURSION

 If a function is recursive, i.e. if the new function is


the same as the one currently being executed,
nothing altered or changed…

 O.S Point of view: The Operating system saves the


status of active function somewhere and starts
executing it as if it was another function , “ofcourse
there is no need to load it in memory again because
the function is already there”

 Which makes the execution vary fast

CCP503.39 16
SUMMARY

 The concept of Recursion in VB

 The behavior and nature of of the Recursion

 Sample program on Recursion Function

 Advantages of Recursion in VB etc,.

CCP503.39 17
FREQUENTLY ASKED QUESTIONS

1. Define Recursion in VB?


2. What are the advantages of Recursion ?
3. Explain the behavior of the Recursion Function in
VB?
4. Write your own program to implement Recursion in
VB ?

CCP503.39 18
QUIZ

1. A recursive procedure is ___ ?

a) A procedure which does not call itself

c) A procedure which calls other procedures

e) A procedure which calls itself

g) Main program that calls some other Procedure

CCP503.39 19
2. Which data structure must be supported by the VB
internally in order to have Recursion concept?

A. Linear Queue
b) Stack
c) Binary Tree
d) Circular Queue

3. From Operating System point of view, Recursion is


advantageous or not?
A. No
B. Yes

CCP503.39 20
ASSIGNMENT
1. Explain the advantages of recursion

CCP503.39 21

You might also like