Functions (Methods) Pascal C, C++ Java Scripting Languages

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
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)
1) Scope a] Ada scope rules b] C scope rules 2) Parameter passing a] Ada parameter modes b) Parameter passing mechanisms COMP205 IMPERATIVE LANGUAGES 13.
The University of Adelaide, School of Computer Science
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.
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
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.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
Pointers CSE 2451 Rong Shi.
SE-1010 Dr. Mark L. Hornick 1 Defining Your Own Classes Part 3.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
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.
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
1 CSE1301 Computer Programming Lecture 12 Functions (Part 1)
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
1 CSE1301 Computer Programming Lecture 13 Functions (Part 1)
3. Methods. Programming Paradigms Procedural Programming ‘Imperative’ assignment used to create state, and procedures manipulate state. E.g., C, Assembly,
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Operators in JAVA. Operator An operator is a symbol that operates on one or more arguments to produce a result. Java provides a rich set of operators.
© A+ Computer Science - In Java, any variable that refers to an Object is a reference variable. The variable stores the memory.
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.
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.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
FUNCTIONS (METHODS) Pascal C, C++ Java Scripting Languages Passing by value, reference Void and non-void return types.
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)
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Parameter passing Module 12.3 COP4020 – Programming Language Concepts Dr. Manuel E. Bermudez.
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 2 Overview of C.
Recursion.
Templates.
INC 161 , CPE 100 Computer Programming
Methods and Parameters
CSE 143 Lecture 9 References and Linked Nodes reading: 3.3; 16.1
March 29th Odds & Ends CS 239.
Pointers  Week 10.
CSI 121 Structured Programming Language Lecture 13 Functions (Part 1)
Subroutines – parameter passing
Recursion.
Java Lesson 36 Mr. Kalmes.
Parameter Passing in Java
5.1 Introduction Pointers Powerful, but difficult to master
Simulating Reference Parameters in C
CSCE 206 Lab Structured Programming in C
Why did the programmer quit his job?
C (and C++) Pointers April 4, 2019.
Chien-Chung Shen CIS/UD
Arrays Arrays A few types Structures of related data items
Arrays.
CMSC 202 Lesson 6 Functions II.
Strings #include <stdio.h>
A+ Computer Science PARAMETERS
Lecture 6: References and linked nodes reading: 16.1
CSCE 206 Lab Structured Programming in C
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; 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 < stdio.h> void main() { int i = 4; i = addTwo(i); } int addTwo(int x) x = x + 2; return (x);

C, C++ void function (aka procedure) #include < stdio.h> void main() { char[] c = “hello”; sayTwice(s); } void sayTwice(char[] x) printf(x);

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

C++ (not Java) passing by reference. #include <stdio.h> void swap(float *x, float *y) { float t; t = *x; /* *x is value pointed to by x */ *x = *y; *y = t; } void main() float a=1, b=2; swap(&a, &b); /* address of x, y */ printf("Values AFTER 'swap' %f, %f\n", a, b); return 0;

Java Methods Java refers 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

Java passes primitives by value only public void addTwo(int x) { x = x+2; } Will not add 2 to the variable passed in to addTwo

This swap will not work in Java public void badSwap(Person x, Person y) { Person temp = x; x = y; y = temp; } Non-primitives are passed as references to copies of the opject. 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.