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.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Introduction to C Programming
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
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
Kernighan/Ritchie: Kelley/Pohl:
Topic 9C – Multiple Dimension Arrays. CISC105 – Topic 9C Multiple Dimension Arrays A multiple dimension array is an array that has two or more dimensions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
Computer Programming Lecture 13 Functions with Multiple Output Parameters Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical & Electronics.
Scope and Casting. Scope Region of the program where a particular name can be referenced Formal parameters and local variables –can be accessed from within.
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.
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.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
C Programming Tutorial – Part I CS Introduction to Operating Systems.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
Modular Programming Advantages of using functions (to make a modular program) are: Changing the program into separate pieces Code reusing Easier modification.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 6 (Pointers) © CPCS
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.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
9/29/99B-1 CSE / ENGR 142 Programming I Variables, Values, and Types © 1998 UW CSE.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
CHAPTER 8 Scope, Lifetime, and More on Functions.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
Problem Solving and Program Design in C Chap. 6 Pointers and Modular Programming Chow-Sing Lin.
C Language Elements Preprocessor Directives # (sign for preprocessor directive commands) #include Standard header file (.h) Library.
Functions, Scope & File IO C++ Lecture 4 Bhaskar Bhattacharya.
Decisions Chapter 4.
C Programming Tutorial – Part I
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Pointers in C.
Chapter 2 Overview of C.
INC 161 , CPE 100 Computer Programming
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Simple Data Types and Function Calls
6 Chapter Functions.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
Arrays, Part 1 of 2 Topics Definition of a Data Structure
The Function Prototype
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Programming Languages and Paradigms
Chapter 6 Modular Programming chap6.
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
FUNCTION ||.
ICS103: Programming in C 6: Pointers and Modular Programming
Presentation transcript:

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 able to return more than one value –In this case, we use output parameters to pass back the additional information –The argument in this case must specify a location to put the value in, not a value

Function with Output Parameters 4 When we want to specify the location to store (e.g.) an integer, we must declare a pointer to an integer void separate(double num, char *signp, int *wholep, double *fracp) { double magnitude; if (num < 0) *signp = ‘-’; else if (num == 0) *signp = ‘0’; else *signp = ‘+’; magnitude = fabs(num); *wholep = floor(magnitude); *fracp = magnitude - *wholep; }

Function with Output Parameters –Now, if we want to call this function, we have to supply a value for the first argument and variables for the second, third, and fourth int main(void) { double value; char sn; int whl; double fr; printf(“Enter a value to analyze> “); scanf(“%lf”, &value); separate(value, &sn, &whl, &fr);

Function with Output Parameters 4 Notice how we specify the address of a variable - with the & operator (as in scanf) 4 In the called function, we must use *var in expressions - otherwise we will be calculating with the address of the variable, not the value!  A declaration such as int *var declares var as a pointer to an integer variable (which is declared somewhere else)

Function with Output Parameters 4 Note that we can pass a number (e.g. 5.24) as the first argument to the function, but we can’t pass numbers (or characters) for the other arguments since they expect addresses not values 4 What happens if we omit the & operator when calling the function?

Meaning of the * Symbol 4 The * symbol has three separate meanings in C –The simple one is the binary multiplication operator: var1 * var2 –In a declaration it means that the variable is a pointer to an element of the given type: char *signp –In the body of a function (e.g. in an expression), it means follow the pointer: *signp = ‘-’; myvar = *ptrvar + 1;

Arguments Used for Both I/O 4 We have seen arguments used for input or for output 4 We can also use a single argument for both input (pass information to the called function) and output (return information to the calling function) –To do this we must use a pointer to a variable –Let’s write a function to add switch the values of two variables

Arguments Used for Both I/O void switch(int *first, int *second) { int holder; holder = *first; *first = *second; *second = holder; } int main(void) { int one = 1, two = 2; /* Initialized! */ printf(“One %d Two %d\n”, one, two); switch(&one, &two); printf(“One %d Two %d\n”, one, two); }

Scope of Names 4 The scope of a name refers to the region of a program where a particular meaning of a name is visible (can be referenced) –We need to understand the scope of functions, variables, and constants –For a constant - #define PI we can use the constant only in the file in which it is declared –Arguments are visible only in the function in which they are declared

Scope of Names –Local variables (variables defined in a function) are visible only in that function –Global variables (variables defined outside of and before all functions) are visible to all functions in the file –Functions are visible to all other functions in the file –Arguments and local variables can be declared with the same name as global functions or variables. In this case, they hide the globals

Scope of Names 4 Functions cannot be nested in C (in many other languages they can, resulting in more complicated scope rules) 4 More complications arise when we use more than one file to implement a program