CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.

Slides:



Advertisements
Similar presentations
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Advertisements

Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
BBS514 Structured Programming (Yapısal Programlama)1 Pointers.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
TDBA66, VT-03, Lecture - Ch6_21 Function calls A function call implies –Every expression in the argument list is evaluated –If necessary, the value of.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
1 ICS103 Programming in C Lecture 10: Functions II.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
1 Review of Chapter 6: The Fundamental Data Types.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
Lecture No: 16. The scanf() function In C programming language, the scanf() function is used to read information from standard input device (keyboard).
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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.
1 Programming with Pointers Turgay Korkmaz Office: SB Phone: (210) Fax: (210) web:
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Modular Programming Advantages of using functions (to make a modular program) are: Changing the program into separate pieces Code reusing Easier modification.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
1 ICS103 Programming in C Lecture 8: Functions I.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Modular Programming Problem Solving and Program Design in C 5th.
1 2/2/05CS250 Introduction to Computer Science II Pointers.
C programming---Pointers The first step: visualizing what pointers represent at the machine level. In most modern computers, main memory is divided into.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
User-Defined Functions (cont’d) - Reference Parameters.
Introduction to C Programming CE Lecture 6 Functions, Parameters and Arguments.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Lecture 5 Pointers 1. Variable, memory location, address, value
CSE 220 – C Programming Pointers.
UNIT 5 C Pointers.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Pointers in C.
Lecture 6 C++ Programming
Variables ,Data Types and Constants
Introduction to Programming
Simple Data Types and Function Calls
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
Lecture 18 Arrays and Pointer Arithmetic
Subroutines – parameter passing
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
C Programming Lecture-8 Pointers and Memory Management
CS150 Introduction to Computer Science 1
C Programming Pointers
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Chapter 6 Modular Programming chap6.
FUNCTION ||.
Presentation transcript:

CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs Concepts of pointers Multiple calls to a function with in/output parameters Scope of variables Formal output parameters as actual arguments Programming with multiple functions

CP104 Introduction to Programming Modular Programming Lecture 16__ 2 Example of functions with one output value #include int max(int,int); int min(int,int); Int main(void) { int a, b, c, x1, x2, x3; scanf("%d%d%d", &a, &b, &c); x1 = max(a, b); x2 = min(a, b); if (c >= x1) { x3 = x2; x2 = x1; x1 = c; } else if (c >= x2) { x3 = x2; x2 = c; } else x3 = c; printf("%d, %d, %d", x3,x2,x1); } int max(int x, int y) { int z; if (x > y) z = x; else z = y; return (z); } int min(int x, int y) { int z; if (x < y) z = x; else z = y; return (z); } When max(a,b) is called during the execution of the main program. 1.It creates two new memory locations for x and y 2.It pass the values of a and b to x and y respectively 3.Compute the max 4.Pass the max value to the main program to assign it to x1 5.Release the memory for x and y

CP104 Introduction to Programming Modular Programming Lecture 16__ 3 Functions with multiple outputs Problem: design a function to output the sign, whole number magnitude, and the fractional part of a floating number Analysis: the function should have the floating number num as a parameter. It should output the sign, magnitude, and fraction part. Sign is “-” if num <0, sign is “ “ if num = 0; magnitude = the absolute value of num; fraction = num – floor of num;

CP104 Introduction to Programming Modular Programming Lecture 16__ 4 Program That Calls a Function with Multiple Output Arguments

CP104 Introduction to Programming Modular Programming Lecture 16__ 5 Program That Calls a Function with Output Arguments (cont’d)

CP104 Introduction to Programming Modular Programming Lecture 16__ 6 Diagram of Function separate with Multiple Results Parameter Correspondence for separate(value, &sn, &whl, &fr);

CP104 Introduction to Programming Modular Programming Lecture 16__ 7 Concepts of Pointers The pointer of a variable means the address of the variable. It is the address of the first memory cell of the variable. –If sn is a char variable, then the address of sn is &sn. –The number of bits of a pointer varies depending on machine. For 32 bits processor, it uses four memory cells. A variable can be defined to hold the pointer of a variable. Such kind of variable is called pointer variable. –Declaration char *signp tells the compiler that signp is a pointer variable, which will contain the address of a type char variable. i.e., signp is a pointer to a char variable. For char pointer signp, the character stored in the memory cell pointed by signp can be derived by *signp The unary operator * is called an indirection operator

CP104 Introduction to Programming Modular Programming Lecture 16__ 8 Concepts of Pointers (cond’d) Examples char sn = ‘a’; /* declare a character variable */ char *signp; /* declare a char pointer variable */ signp = &sn; /* assign the address of sn to signp */ printf(“%c”, *signp); /* equivalent to printf(“%c”, sn); */ –A pointer variable must point to a variable of the type it declared char sn = ‘a’; /* declare a character variable */ double *d; /* declare a pointer of double variable */ d = &sn; /* this is a wrong assignment! */

CP104 Introduction to Programming Modular Programming Lecture 16__ 9 Example of Pointers #include main)() { int a = 100, b = 10; int *pointer1, *pointer2; pointer1 = &a; pointer2 = &b; printf(“%d, %d\n”,a, b); printf(“%d, %d\n”,*pointer1, *pointer2); } The output will be 100,10

CP104 Introduction to Programming Modular Programming Lecture 16__ 10 Example Input two integers a and b, compare and display them in decreasing order #include main() { int *p1, *p2, *p, a, b; scanf("%d,%d", &a,&b); p1 = &a; p2 = &b; if (a < b) { p = p1; p1 = p2; p2 = p; } printf("\na=%d, b=%d\n", a, b); printf("max = %d, min = %d\n",*p1, *p2); } Input: 5, 9 Display: a = 5, b =9 max = 9, min = 5 &a &b 5 9 p1 p2 a b p &b &a 5 9 p1 p2 a b p