Assist.Prof.Dr. Nükhet ÖZBEK Ege University

Slides:



Advertisements
Similar presentations
Lectures 10 & 11.
Advertisements

Programming and Data Structure
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
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.
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.
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 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Computer Science 210 Computer Organization Pointers.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
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)
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.
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.
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.
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.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
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.
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
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.
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
User-Written Functions
Chapter 7: User-Defined Functions II
ICS103 Programming in C Lecture 10: Functions II
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
EPSII 59:006 Spring 2004.
Pointers and Pointer-Based Strings
Pointers.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Pointers in C Good morning Ladies and Gentlemen. Welcome to this talk on “Pointers in C”
Simple Data Types and Function Calls
Functions.
Pointers Problem Solving & Program Design in C Eighth Edition
Programming in C Pointer Basics.
Programming with Pointers
Functions, Part 2 of 3 Topics Functions That Return a Value
Subroutines – parameter passing
Reference Parameters.
Programming in C Pointer Basics.
Simulating Reference Parameters in C
Pointers and Pointer-Based Strings
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
CS150 Introduction to Computer Science 1
ICS103 Programming in C Lecture 10: Functions II
Introduction to Computing Lecture 08: Functions (Part I)
ETE 132 Lecture 6 By Munirul Haque.
Chapter 6 Modular Programming chap6.
Functions, Part 2 of 3 Topics Functions That Return a Value
ICS103 Programming in C Lecture 10: Functions II
Functions, Part 2 of 3 Topics Functions That Return a Value
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 nukhet.ozbek@ege.edu.tr

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

Example #include <stdio.h> 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

Before calling funct1: u=1 v=3 Within funct1: u=0 v=0 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

Before calling funct2: u=1 v=3 Within funct2: *pu=0 *pv=0 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)

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 Multiplication operation (a * b) Pointer declaration char *signp; Read * as “pointer to” 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 = Figure 6.7 Data Areas After temp = *smp; During Call order(&num1, &num3);

Different Kinds of Functions Purpose Function Type Parameters To 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 void No result is returned To compute multiple results 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 Input/Output parameters are pointers to actual arguments