Subroutines sub { –#parameters are placed – –. –return; }

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
1 Procedure Calls, Linking & Launching Applications Lecture 15 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
1 5.3 Sub Procedures, Part II Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging.
Procedures and Stacks. Outline Stack organization PUSH and POP instructions Defining and Calling procedures.
Programming and Perl for Bioinformatics Part III.
9.1 Subroutines and sorting. 9.2 A subroutine is a user-defined function. Subroutine definition: sub SUB_NAME { STATEMENT1; STATEMENT2;... } Subroutine.
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Perl Functions Software Tools. Slide 2 Defining a Function l A user-defined function or subroutine is defined in Perl as follows: sub subname{ statement1;
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
EECC250 - Shaaban #1 Lec # 5 Winter Stacks A stack is a First In Last Out (FILO) buffer containing a number of data items usually implemented.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Scripting Languages Perl Chapter #4 Subroutines. Writing your own Functions Functions is a programming language serve tow purposes: –They allow you to.
Lecture 2 BNFO 135 Usman Roshan. Perl variables Scalar –Number –String Examples –$myname = “Roshan”; –$year = 2006;
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Perl Functions Learning Objectives: 1. To learn how to create functions in a Perl’s program & how to call them 2. To learn how to pass [structured] arguments.
Subroutines. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc We’ll just say Subroutines. –“Functions” generally means built-in.
1 Classes and Objects. 2 Outlines Class Definitions and Objects Member Functions Data Members –Get and Set functions –Constructors.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Lecture 8: Basic concepts of subroutines. Functions In perl functions take the following format: – sub subname – { my $var1 = $_[0]; statements Return.
subroutines and references
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Prof. Alfred J Bird, Ph.D., NBCT Office – McCormack 3rd floor 607.
CS 2104 Prog. Lang. Concepts Subprograms
ITEC 352 Lecture 18 Functions in Assembly. Functions + Assembly Review Questions? Project due on Friday Exam –Average 76 Methods for functions in assembly.
MIPS function continued. Recursive functions So far, we have seen how to write – A simple function – A simple function that have to use the stack to save.
PASSING VALUE TO A FUNCTION # CALL BY VALUECALL BY VALUE # CALL BY REFERENCECALL BY REFERENCE STORAGE CLASS # AUTOAUTO # EXTERNALEXTERNAL # STATICSTATIC.
I Power Int 2 Computing Software Development High Level Language Constructs.
Procedures. Why use procedures? ? Microprocessors (and assembly languages) provide only minimal support for procedures Must build a standard form for.
PHP Constructs Advance Database Management Systems Lab no.3.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
I Power Higher Computing Software Development High Level Language Constructs.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
Perl Chapter 6 Functions. Subprograms In Perl, all subprograms are functions – returns 0 or 1 value – although may have “side-effects” optional function.
Topic 4:Subroutines CSE2395/CSE3395 Perl Programming Learning Perl 3rd edition chapter 4, pages 56-72, Programming Perl 3rd edition pages 80-83,
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
Scripting Languages Diana Trandab ă ț Master in Computational Linguistics - 1 st year
Functions & Pointers in C Jordan Erenrich
Department of Electrical and Computer Engineering Introduction to Perl By Hector M Lugo-Cordero August 26, 2008.
***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Lecture 5 functions 1 © by Pearson Education, Inc. All Rights Reserved.
Week 8 - Friday.  What did we talk about last time?  Static methods.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
C Programming Chapters 11, . . .
ENEE150 – 0102 ANDREW GOFFIN More With Pointers. Importance of Pointers Dynamic Memory (relevant with malloc) Passing By Reference Pointer Arithmetic.
PZ09A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ09A - Activation records Programming Language Design.
Announcements There is a Quiz today. There were problems with grading assignment 2, but they should be worked out today The web page for correcting the.
PROGRAMMING 1 – HELPER INSTRUCTIONS ACKNOWLEDGEMENT: THE SLIDES ARE PREPARED FROM SLIDES PROVIDED BY NANCY M. AMATO AND JORY DENNY 1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
More on Variables and Subroutines. Introduction Discussion so far has dealt with self- contained subs. Subs can call other subs or functions. A module.
BINF 634 Fall LECTURE061 Outline Lab 1 (Quiz 3) Solution Program 2 Scoping Algorithm efficiency Sorting Hashes Review for midterm Quiz 4 Outline.
EGRE 6311 LHO 04 - Subprograms, Packages, and Libraries EGRE 631 1/26/09.
Computer Science 210 Computer Organization
Introduction to Perl: Part II
Function There are two types of Function User Defined Function
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
FIGURE 9-5 Integer Constants and Variables
Pointers and References
Subroutines Web Programming.
Int add3 (int a, int b, int c) { return a + b + c; } int sum = 0; main () sum += add3 (1, 2, 3); sum += 10; sum += add3 (10, 20, 30);
Variables and Their scope
Passing Parameters Data passed to a subroutine is called a parameter.
Subroutines – parameter passing
Warm Up Combine like terms: 6x – 2xy – 7z + 4 – 3y + x – 7xy + 3y xz.
Adding and Subtracting Polynomials.
NQC Program Structure 3 types of code blocks with their own features and limitations 1. Tasks 2. Subroutines 3. Inline Functions.
Presentation transcript:

Subroutines sub { –#parameters are placed – –. –return; }

Scope You can create local variables using my. Otherwise all variables are assumed global, i.e. they can be accessed and modified by any part of the code. Local variables are only accessible in the scope of the code.

Pass by value and pass by reference All variables are passed by value to subroutines. This means the original variable lying outside the subroutine scope does not get changed. You can pass arrays by reference using \. This is the same as passing the memory location of the array.

Pass by value $a = &add($x,$y); sub add { my $x=$_[0]; my $y = $_[1]; return $x+$y; }

Pass by value ($a,$b) = &sum_prod($x,$y); sub sum_prod{ my $x=$_[0]; my $y=$_[1]; $s = $x+$y; $p = $x*$y; return ($s,$p); }

Pass by = &sum_prod($x,$y); #sum in $a[0], product in $a[1] sub sum_prod{ my $x=$_[0]; my $y=$_[1]; $s = $x+$y; $p = $x*$y; return ($s,$p); }

Pass by = &get_array; sub get_array{ }

Passing = &get_array2; will be empty sub get_array2{ #this concatenated into one array }

Pass by reference ($a, $b) = &get_array2; #to access the array we #the same applies to hashes except that hashes are dereferences #using %$a and %$b sub get_array2{ return #this returns references (pointers) }