Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters.

Slides:



Advertisements
Similar presentations
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Advertisements

Functions Prototypes, parameter passing, return values, activation frams.
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Programming Languages and Paradigms The C Programming Language.
Def f(n): if (n == 0): return else: print(“*”) return f(n-1) f(3)
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Pointers Discussion 5 Section Housekeeping HW 1 Issues Array Issues Exam 1 Questions? Submitting on Time!
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
CIS 101: Computer Programming and Problem Solving Lecture 9 Usman Roshan Department of Computer Science NJIT.
Java Syntax Primitive data types Operators Control statements.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
Overview Pointer Variables Pass by Value Pass by Reference (or by Pointer) Arrays.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 16 Exceptions,
Computer Science 210 Computer Organization Pointers.
Computer Science 210 Computer Organization Strings in C.
Operator Overloading CS 308 – Data Structures What is operator overloading? Changing the definition of an operator so it can be applied on the objects.
Imperative Programming. Heart of program is assignment statements Aware that memory contains instructions and data values Commands: variable declarations,
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Scott Marino MSMIS Kean University MSAS5104 Programming with Data Structures and Algorithms Week 10 Scott Marino.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Computer Science 210 Computer Organization Arrays.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
208 New Book Review. String Type #include Assignment – string m=“Hi Mom”; – m=“Goodbye”; Concatenate - + – Can use variables or literals. Can use [ ]
File IO and command line input CSE 2451 Rong Shi.
1 ร. ศ. ดร. สุเทพ มาดารัศมี Understanding Pointers in C Chapter 10 of Programming with C Book.
Java Basics.  To checkout, use: svn co scb07f12/UTORid  Before starting coding always use: svn update.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 CSE 2341 Object Oriented Programming with C++ Note Set #2.
Lecture 6 C++ Programming Arne Kutzner Hanyang University / Seoul Korea.
Basics and arrays Operators:  Arithmetic: +, -, *, /, %  Relational:, >=, ==, !=  Logical: &&, ||, ! Primitive data types Byte(8), short(16), int(32),
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
© Oxford University Press All rights reserved. CHAPTER 6 STRINGS.
Functions & Pointers in C Jordan Erenrich
Pointers *, &, array similarities, functions, sizeof.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
1 Chapter 12 Arrays. 2 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating.
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---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
2/23/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 9 (C-3)
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.
Announcements You will receive your scores back for Assignment 2 this week. You will have an opportunity to correct your code and resubmit it for partial.
CS 1430: Programming in C++ 1. Test 2 Friday Functions Arrays For Loops Understand Concepts and Rules Memorize Concepts and Rules Apply Concepts and Rules.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Pointers  * symbol and & symbol  Pointer operations  Pointer.
1 Chapter 7 Pointers and C-Strings. 2 Objectives  To describe what a pointer is (§7.1).  To learn how to declare a pointer and assign a value to it.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Current Assignments Project 3 has been posted, due next Tuesday. Write a contact manager. Homework 6 will be posted this afternoon and will be due Friday.
Imperative Programming C. Imperative Programming Heart of program is assignment statements Aware that memory contains instructions and data values Commands:
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Pointers A variable that holds an address value is called a pointer variable, or simply a pointer.  What is the data type of pointer variables? It’s not.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Chapter 7 User-Defined Methods.
Computer Science 210 Computer Organization
Introduction to Programming Using C
CSE 220 – C Programming Pointers.
Recursion.
Recursion.
Computer Science 210 Computer Organization
Recursion.
Pass by Reference.
C++ Pointers and Strings
登金陵鳳凰臺 ~李白 鳳凰臺上鳳凰遊, 鳳去臺空江自流。 吳宮花草埋幽徑, 晉代衣冠成古丘。 三山半落青山外, 二水中分白鷺洲。
C++ Pointers and Strings
C Pointers Another ref:
Presentation transcript:

Comp 255: Lab 02 Parameter Passing pass by value pass by reference In, out, in/out and array parameters

int readInt()// reads/inputs unsigned integer { char str[80]; int i; int number; i = 0; str[i] = getchar(); A1: if (str[i] == '\n') goto A2; i++; str[i] = getchar(); goto A1; A2: str[i] = '\0'; // convert to integer via Horner's Method number = 0; i = 0; A3: if (str[i] == '\0') goto A4; number = number * 10 + (str[i] - 48); i++; goto A3; A4: return(number); }

int printInt(int number)// prints unsigned integer { char str[80]; int i; i = 0; C1: str[i] = (number % 10) + 48; number = number / 10; i++; if (number > 0)// bottom test loop goto C1; // print out string backwards C3: if (i == 0) goto C4; i--; putchar(str[i]); goto C3; C4: return(0); }

int printStr(char s[]) // prints cstring { int i = 0; P1: if (s[i] == '\0') goto P2; putchar(s[i]); i++; goto P1; P2: return(0); }

Pass By Reference in C In pass by reference the address of a variable is passed; not the value of a variable In the definition of the function this is indicated by prefixing a *, the pointer operator, to the formal parameter int fubar(int *x, int *y) So in the call of the function you must explicitly pass the address of the variable using the & operator err = fubar(&a, &b)

Pass by Reference in C In the definition of the function you must dereference all pass by reference parameters to obtain the value of the parameter using the * dereference operator. int fubar(int *x, int *y) { int q, r; if (*y == 0) goto F1; q = *x / *y; r = *x % *y; *x = q; *y = r; return(0); F1: return(1); // error! }

max01 function example pass by value; function returns a value int max01(int a, int b) { if (a > b) return a; else return b; } int main() { int a, b, c; c = max01(a, b); // function call Function definition

max02 function example int max02(int a, int b, int *c) { if (a > b) c* = a; else c* = b; } int main() { int a, b, c; max02(a, b, &c); //function call Function definition

Four Logical Types of Parameters in parameters: used to pass values into a function; call by value out parameters: used to return values from a function; call by reference in/out parameters: parameters will are passed in to a function, modified and returned to the calling program; call by reference array parameters: parameter is an array type; syntactically pass by value

Notes