Presentation is loading. Please wait.

Presentation is loading. Please wait.

Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik.

Similar presentations


Presentation on theme: "Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik."— Presentation transcript:

1 Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik – , India

2 SBI083-CP2-05 Introduction Programmes and Courses  SEP – SBI083-CP2-05

3 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.3 Credits  Academic Inputs by Sonali Alkari MSc (Botany), P.G. D.C. Bio-Informatics

4 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.4 How to Use This Resource  Counselor at each study center should use this presentation to deliver lecture of minutes during Face-To-Face counseling.  Discussion about students difficulties or tutorial with assignments should follow the lecture for about minutes.  Handouts (with 6 slides on each A4 size page) of this presentation should be provided to each student.  Each student should discuss on the discussion forum all the terms which could not be understood. This will improve his writing skills and enhance knowledge level about topics, which shall be immensely useful for end exam.  Appear several times, for all the Self-Tests, available for this course.  Student can use handouts for last minutes preparation just before end exam.

5 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.5 Learning Objectives  After studying this module, you should be able to: Discuss System and User Functions Describe Subroutine with examples Describe Lexical Variables and local operator State applications of subroutines

6 School of Science and Technology, Online Counseling Resource… System and User Functions  Subroutines are an important way to organize a program and use in all major programming languages.  A subroutine wraps up a bit of code, gives the code a name, and provides a way to pass in some values for its calculations and then report back the results.  The rest of the program can then use the subroutine’s code just by calling its name, giving the needed value to pass in to the subroutine code and then collecting the results.  Perl has the ability to make subroutines, which are user-defined functions. These let us recycle one chunk of code many times in one program. © 2007, YCMOU. All Rights Reserved.6

7 School of Science and Technology, Online Counseling Resource… Defining a Subroutine-1  To define your own subroutine, use the keyword sub, the name of the subroutine (without the ampersand), then the indented block of code (in curly braces) which makes up the body of the subroutine, something like this: sub marine { $n += 1; # Global variable $n print "Hello, sailor number $n!\n"; }  Subroutine definitions can be anywhere in your program text, but programmers who come from a background of languages like C or Pascal like to put them at the start of the file. © 2007, YCMOU. All Rights Reserved.7

8 School of Science and Technology, Online Counseling Resource… Defining a Subroutine-2  Others may prefer to put them at the end of the file, so that the main part of the program appears at the beginning.  The use or invocation of a subroutine is commonly referred to as calling the subroutine.  To use a subroutine, you pass data into subroutines as arguments, and then you collect the return value(s) of the subroutine.  When calling a subroutine, older versions of perl required starting of a subroutine with the ampersand character, but these days ampersand is usually omitted. © 2007, YCMOU. All Rights Reserved.8

9 School of Science and Technology, Online Counseling Resource… Demonstration of subroutine #!/usr/bin/perl $DNA='CCTTAAGG'; $longer_dna= addACGT($DNA); print" I added ACGT to $DNA and got $longer_dna\n\n"; exit; sub addACGT { $DNA.='ACGT'; return $DNA; } The produce of this program is I added ACGT to CCTTAAGG and got CCTTAAGGACGT © 2007, YCMOU. All Rights Reserved.9

10 School of Science and Technology, Online Counseling Resource… Explanation  In this example a subroutine that given some DNA, appends “ACGT” to the end of the DNA and return new longer DNA.  This program has two sections. The first section starts from the beginning of the program and ends with the exit command, this part is called as the main program or main body of the program.  Following the main program is a section for subroutine definition, in this case only one definition for subroutine addACGT.  The subroutine is called by its name, followed by parentheses containing the arguments to the subroutine. © 2007, YCMOU. All Rights Reserved.10

11 School of Science and Technology, Online Counseling Resource… The Local Operator-1  Mostly in older code or older Perl books, you'll see the local operator used instead of my. It often looks much the same as my: sub max { local($a, $b) # looks a lot like my if ($a > $b) { $a } else { $b } }  But local is misnamed, or at least misleadingly named. So it was decided to call local by the name "save" instead.  That's because local actually will save the given global variable's value away, so it will later automatically be restored to the global variable. © 2007, YCMOU. All Rights Reserved.11

12 School of Science and Technology, Online Counseling Resource… The Local Operator-2  What local actually does, is to save away a copy of the variable's value in a secret place (called the stack). That value can't be accessed, modified, or deleted while it is saved.  Then local sets the variable to an empty value (undef for scalars, or empty list for arrays), or to whatever value is being assigned.  When Perl returns from the subroutine, the variable is automatically restored to its original value.  In effect, the variable was borrowed for a time and given back (hopefully) before anyone noticed that it was borrowed. © 2007, YCMOU. All Rights Reserved.12

13 School of Science and Technology, Online Counseling Resource… Lexical Variables  However, little smarter, as well as being shorter, operator, my is used.  It is just like variable $office which is private, and private $office can't be accessed from outside his subroutine.  Barney didn't change the way &say works, which is more like what most programmers would want and expect.  In any new code, just use my, since my variables (lexical variables) are faster than globals--remember, so-called local variables are really globals--and they'll work more like the traditional variables in other modern programming languages. © 2007, YCMOU. All Rights Reserved.13

14 School of Science and Technology, Online Counseling Resource… The Local Operator-3  But when you're maintaining someone else's old code, you can't necessarily change every local to my without checking upon whether the programmer was using that save-and- restore functionality. © 2007, YCMOU. All Rights Reserved.14

15 School of Science and Technology, Online Counseling Resource… Variable-length Parameter Lists-1  In real-world Perl code, subroutines are often given parameter lists of arbitrary length.  That's because of Perl's "no unnecessary limits" philosophy that we've already seen.  Of course, this is unlike many traditional programming languages, which require every subroutine to be strictly typed; that is, to permit only a certain, predefined number of parameters of predefined types.  It's nice that Perl is so flexible, but (as we saw with the &max routine earlier) that may cause problems when a subroutine is called with a different number of arguments than the author expected. © 2007, YCMOU. All Rights Reserved.15

16 School of Science and Technology, Online Counseling Resource… Variable-length Parameter Lists-2  Of course, the subroutine can easily check that it has the right number of arguments by examining array.  For example, we could have written &max to check its argument list like this: sub max { if != 2) { print "WARNING! &max should get exactly two arguments!\n"; }  But in real-world Perl programming, this sort of check is hardly ever used; it's better to make the subroutine adapt to the parameters. © 2007, YCMOU. All Rights Reserved.16

17 School of Science and Technology, Online Counseling Resource… Advantages of Subroutines-1  Subroutines provide several benefits. They endow program with abstraction, modularization and the ability to create large programs by organizing the code into manageable chunks with defined inputs and outputs  Subroutines can themselves call other subroutine.  By writing subroutine, you make your program: Shorter, since you are reusing the code. Easier to test, since you can test subroutine separately. Easier to understand, since it reduces clutter and better organize programs. © 2007, YCMOU. All Rights Reserved.17

18 School of Science and Technology, Online Counseling Resource… Advantages of Subroutines-2 More reliable, since you have less code when you reuse subroutines, so there are fewer opportunities for something to go wrong. Faster to write, since you may, for example, have already written some subroutine that handles basic statistics and can the one that calculates the meaning without having to write it again. Or you found a good statistics library someone else wrote, and you never had write it at all.  Decomposing problems into sets of subroutines that can be conveniently combined allows you to create environment that can grow and adapt changing condition with minimum of efforts. © 2007, YCMOU. All Rights Reserved.18

19 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.19 What You Learn-1…  Perl has the ability to make subroutines, which are user-defined functions.  The subroutine is called by its name, followed by parentheses containing the arguments to the subroutine.  Subroutine definitions can be anywhere in your program text.  The use of operator, my is just like variable $office which is private, and private $office can't be accessed from outside his subroutine and it is called as local operator.  my variables (lexical variables) are faster than globals—remember.

20 School of Science and Technology, Online Counseling Resource… What You Learn-2…  Subroutines endow program with abstraction, modularization and the ability to create large programs by organizing the code into manageable chunks with defined inputs and outputs. © 2007, YCMOU. All Rights Reserved.20

21 School of Science and Technology, Online Counseling Resource… Critical Thinking Questions 1.Explain subroutine in details with example. 2.Write a detailed note on subroutines applications. 3.Write a short note on my opertor. © 2007, YCMOU. All Rights Reserved.21

22 School of Science and Technology, Online Counseling Resource… Hints For Critical Thinking Question 1.Describe what is subroutine, how and where to write subroutine in perl program with example. 2.Write a detailed note on subroutine advantages. 3.Use and syntax of my. © 2007, YCMOU. All Rights Reserved.22

23 School of Science and Technology, Online Counseling Resource… Hints For Critical Thinking Question Perl Availability Downloading and installation of perl Window perl, unix,linux based perl and Macs based Perl Program © 2007, YCMOU. All Rights Reserved.23

24 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.24 Study Tips:Books  Beginning Perl Simon Cozens, Peter Wainwright. 700 pages. Wrox Press Inc. 5, 2000). Beginning Perl  Impatient Perl Greg London (Feb 7, 2004). Impatient Perl  Extreme Perl Robert Nagler Extreme Perl  MacPerl: Power & Ease Vicky Brown and Chris Nandor. (1998). MacPerl: Power & Ease  Picking Up Perl Bradley M. Kuhn and Neil Smyth. self published. second edition. (July 2005). Picking Up Perl

25 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.25 Study Tips:Web Resources Microsoft Encarta Encyclopedia

26 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.26 Community Web Sites  Planet Perl - an aggregation of selected perl journals Planet Perl  use Perl; - read community news and personal journals, or start your own journal use Perl;  Perl Monks - the wisdom of the Monks can guide you on your Perl Quests Perl Monks  perl.org - your current location perl.org  the Perl Apprenticeship Site the Perl Apprenticeship Site

27 School of Science and Technology, Online Counseling Resource… © 2007, YCMOU. All Rights Reserved.27 End of the Presentation Thank You


Download ppt "Online Counseling Resource YCMOU ELearning Drive… School of Architecture, Science and Technology Yashwantrao Chavan Maharashtra Open University, Nashik."

Similar presentations


Ads by Google