Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.

Slides:



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

Lectures 10 & 11.
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Subroutines – parameter passing passing data to/from a subroutine can be done through the parameters and through the return value of a function subroutine.
Programming and Data Structure
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
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.
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
Pointer A variable that represent the location (rather than value) of a data item, such as variable or an array element It is used to pass information.
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 A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
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.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
Computer Science 210 Computer Organization Pointers.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 14P. 1Winter Quarter Pointers Lecture 14.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Pointers Chapter 9. Getting The Address Of A Variable Each variable in program is stored at a unique address Use address operator & to get address of.
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.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
1 Pointers and Strings Chapter 5 2 What You Will Learn...  How to use pointers Passing arguments to functions with pointers See relationship of pointers.
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
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:
Engineering Problem Solving with C Fundamental Concepts Chapter 7 Structures.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Modular Programming Problem Solving and Program Design in C 5th.
POINTERS IN C. Introduction  A pointer is a variable that holds a memory address  This address is the location of another object (typically another.
17-Feb-02 Sudeshna Sarkar, CSE, IT Kharagpur1 Arrays and Pointers Lecture 17 18/2/2002.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Computer Programming Lecture 12 Pointers Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering
Pointers 1. Introduction Declaring pointer variables Pointer operators Pointer arithmetic 2 Topics to be Covered.
User-Defined Functions (cont’d) - Reference Parameters.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
CCSA 221 Programming in C CHAPTER 3 COMPILING AND RUNNING YOUR FIRST PROGRAM 1 ALHANOUF ALAMR.
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
Chapter 7: User-Defined Functions II
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Pointers.
Lecture 6 C++ Programming
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Pointers Problem Solving & Program Design in C Eighth Edition
Programming with Pointers
Subroutines – parameter passing
Reference Parameters.
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Simulating Reference Parameters in C
CS150 Introduction to Computer Science 1
Introduction to Computing Lecture 08: Functions (Part I)
Chapter 6 Modular Programming chap6.
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics Engineering

Topics Functions with Simple Output Parameters Formal Output Parameters as Actual Arguments

Pointers The correct understanding and use of pointers is crucial to successful C programming. There are several reasons for this: –Pointers provide the means by which functions can modify their calling arguments –Pointers support dynamic allocation –Pointers can improve the efficiency of certain routines –Pointers provide support for dynamic data structures, such as binary trees and linked lists

Pointers Pointers are one of the strongest but also one of the most dangerous features in C. For example, a pointer containing an invalid value can cause your program to crash Perhaps worse, it is easy to use pointers incorrectly, causing bugs that are very difficult to find

Pointers Pointers are used frequently in C, as they have a number of useful applications. –For example, pointers can be used to pass information back and forth between a function and its reference point In particular, pointers provide a way to return multiple data items from a function via function arguments Pointers also permit references to other functions to be specified as arguments to a given function. This has the effect of passing functions as arguments to the given function

Passing Pointers to a Function Pointers are often passed to a function as arguments This allows data items within the calling portion of the program to be accessed by the function, altered within the function, and then returned to the calling portion of the program in altered form We refer to this use of pointers as passing arguments by reference (or by address or by location), in contrast to passing arguments by value

Passing Pointers to a Function When an argument is passed by value, the data item is copied to the function. Thus, any alteration made to the data item within the function is not carried over into the calling routine When an argument is passed by reference, however (i.e., when a pointer is passed to a function), the address of a data item is passed to the function. Any change that is made to the data item (i.e., to the contents of the address) will be recognized in both the function and the calling routine

#include void funct1( int u, int v); / * function prototype */ void funct2( int *pu, int *pv); /* function prototype */ main () { int u = 1, v = 3; printf("\nBefore calling funct1: u=%d v=%d", u, v); funct1(u, v); printf( "\nAfter calling funct1: u=%d v=%d", u, v); printf(“\n\nBefore calling funct2: u=%d v=%d“, u,v); funct2(&u, &v); printf( "\nAfter calling funct2: u=%d v=%d", u, v); } void funct1(int u, int v) { u = 0; v = 0; printf( "\nWithin funct1: u=%d v=%d", u, v); } void funct2(int *pu, int *pv) { *pu = 0; *pv = 0; printf( "\nWithin funct2: *pu=%d *pv=%d",*pu,*pv); } Example

main () { int u = 1, v = 3; printf("\nBefore calling funct1: u=%d v=%d", u, v); funct1(u, v); printf( "\nAfter calling funct1: u=%d v=%d", u, v); printf(“\n\nBefore calling funct2: u=%d v=%d“, u,v); funct2(&u, &v); printf( "\nAfter calling funct2: u=%d v=%d", u, v); } void funct1(int u, int v) { u = 0; v = 0; printf( "\nWithin funct1: u=%d v=%d", u, v); } Example Before calling funct1: u=1 v=3 Within funct1: u=0 v=0 After calling funct1: u=1 v=3

main () { int u = 1, v = 3; printf("\nBefore calling funct1: u=%d v=%d", u, v); funct1(u, v); printf( "\nAfter calling funct1: u=%d v=%d", u, v); printf(“\n\nBefore calling funct2: u=%d v=%d“, u,v); funct2(&u, &v); printf( "\nAfter calling funct2: u=%d v=%d", u, v); } void funct2(int *pu, int *pv) { *pu = 0; *pv = 0; printf( "\nWithin funct2: *pu=%d *pv=%d",*pu,*pv); } Example Before calling funct2: u=1 v=3 Within funct2: *pu=0 *pv=0 After calling funct2: u=0 v=0

Returning Multiple Results from a Function So far, we have used return statement to return one result from a function Pointers can be used to return multiple results

Function separate

Diagram of Function separate with Multiple Results

Figure 6.3 Program That Calls a Function with Output Arguments

Program That Calls a Function with Output Arguments (cont’d)

Parameter Correspondence for separate(value, &sn, &whl, &fr);

Comparison of Direct and Indirect Reference

Meanings of * Symbol 1.Multiplication operation (a * b) 2.Pointer declaration char *signp; Read * as “pointer to” 3.Unary * *signp = ‘+’; Means “follow the pointer” Careful! Unary * does not mean “pointer to”

Multiple Calls to a Function with Input/Output Parameters So far, we have passed information into a function through its input parameters and returned results from a function through its output parameters We can also use a single parameter both to bring data value into a function and carry the result out of the function

Figure 6.6 Program to Sort Three Numbers

Figure 6.6 Program to Sort Three Numbers (cont’d)

Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);

Different Kinds of Functions PurposeFunction TypeParametersTo Return Result To compute a single value Same as type of value to be computed Input parameters hold copies of data provided Function includes a return statement To produce printed output voidInput parameters hold copies of data provided No result is returned To compute multiple results voidInput parameters hold copies of data provided Output parameters are pointers to actual arguments Results are stored in the calling function’s data area by indirect assignment. No return statement is required To modify argument values voidInput/Output parameters are pointers to actual arguments Results are stored in the calling function’s data area by indirect assignment. No return statement is required