FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.

Slides:



Advertisements
Similar presentations
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)
Advertisements

Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Procedures and Control Flow CS351 – Programming Paradigms.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
David Notkin Autumn 2009 CSE303 Lecture 10 "If it weren't for C, we'd be writing programs in BASI, PASAL, and OBOL."
Pointers and Output Parameters. Pointers A pointer contains the address of another memory cell –i.e., it “points to” another variable cost:1024.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Introduction to C Programming CE
1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays.
1 Methods Overview l Closer Look at Methods l Parameter passing l Passing parameters by value l Passing parameters by reference.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
1 Parameter Passing Revisited Overview l Parameter passing l Passing parameters by value l Passing parameters by reference l Some Examples l Preview: Introduction.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Pointers CSE 2451 Rong Shi.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
C Programming Lecture 8-2 : Function (advanced). Recursive Function (recursion) A function that calls itself (in its definition) Classic example : factorial.
UNIT 14 Functions with Pointer Parameters.
Scope: Portion of the program in which the identifier can be referenced. Various types of scope, my examples are of block scope and global scope.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
Copyright © 2006 The McGraw-Hill Companies, Inc. Basic Terminology Value-returning functions: –known as “non-void functions/methods” in C/C++/Java –called.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Mark Fontenot CSE Honors Principles of Computer Science I Note Set 7.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Functions (Methods) Pascal C, C++ Java Scripting Languages
CSE 220 – C Programming Pointers.
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Building Java Programs
Principles of programming languages 4: Parameter passing, Scope rules
Pointers and Pointer-Based Strings
Functions, variables, operators, loops Modularity
INC 161 , CPE 100 Computer Programming
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
March 29th Odds & Ends CS 239.
Mr. Dave Clausen La Cañada High School
Building Java Programs
An Introduction to Java – Part II
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Subroutines – parameter passing
Java Lesson 36 Mr. Kalmes.
Parameter Passing in Java
CS2011 Introduction to Programming I Methods (II)
CSE 3302 Programming Languages
Simulating Reference Parameters in C
METHODS, CLASSES, AND OBJECTS A FIRST LOOK
Why did the programmer quit his job?
Pointers and Pointer-Based Strings
Take out a piece of paper and PEN.
CS150 Introduction to Computer Science 1
CSE 303 Lecture 10 C memory model; stack allocation
Take out a piece of paper and PEN.
CMSC 202 Lesson 6 Functions II.
A+ Computer Science PARAMETERS
Lecture 6: References and linked nodes reading: 16.1
Presentation transcript:

FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types

Functions in Mathematics F(X) = X + 2; Mathematics Question) What is F(4)? Answer) 6 Computer Programming question X = 4; X = F(X); Output (X); Question) What is the output of this program? Answer) 6

Functions in Pascal program functionExample var i: Integer; function addTwo(x:Integer): Integer; begin addTwo = x + 2; end; begin i = 4; i = addTwo(i); writeln(i); end;

Procedures in Pascal program procedureExample procedure sayTwice(s: String); var i: integer; begin for i := 1 to 2 do Writeln(s); end; begin sayTwice(“Hello”); end;

Pascal passing by value program procedureExample var i:Integer; procedure addTwo(x: Integer); begin x := x + 2; end; begin i:=4: addTwo(i); Writeln(i); end; The output will be: 4 (not 6) because x and i are not referring to the same variable.

Pascal passing by reference program procedureExample var i:Integer; procedure addTwo(var x: Integer); begin x := x + 2; end; begin i:=4: addTwo(i); Writeln(i); end; The output will be: 6 (not 4) because x and i are referring to the same variable because “var” was added in front of x.

C, C++ functions #include void main() { int i = 4; addTwo(i); cout << i; } void addTwo(int x) { x = x + 2; }

C, C++ void function (procedure) #include void main() { String s = “hello”; sayTwice(s); } void sayTwice(String x) { printf(x); }

C, C++ passing by value #include void main(void) { int i = 4; addTwo(i); printf(i); } void addTwo(int x) { x = x + 2; }

C++ (not C) passing by reference. #include void swap(int &i, int &j) { int temp = i; i = j; j = temp; } int main(void) { int a = 10; int b = 20; swap(a, b); printf("A is %d and B is %d\n", a, b); return 0; }

Java Methods Java refer to functions as Methods. All Methods must belong to a class (unlike C, C++) Return types including void are the same as C, C++ Parameters are passed by value only for Primitive Types. By reference but to temporary storgage.

Java passes parameters by value only public void addTwo(int x) { x = x+2; } Will not swap the values passed into it.

This swap will not work in Java public void badSwap(Person x, Person y) { Person temp = x; x = y; y = temp; } Although x and y are references, they refer to a copy of the object that the calling parameter is referring to, not the same object.

C# static void SquareIt(int x) // The parameter x is passed by value. // Changes to x will not affect the original value of myInt. static void SquareIt(ref int x) // The parameter x is passed by reference. // Changes to x will affect the original value of myInt.