IPC144 Introduction to Programming Using C Week 6 – Lesson 1

Slides:



Advertisements
Similar presentations
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Advertisements

1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
An Introduction to Programming with C++ Fifth Edition Chapter 10 Void Functions.
Chapter 10 More on Modular Programming and Functions.
Topic R3 – Review for the Final Exam. CISC 105 – Review for the Final Exam Exam Date & Time and Exam Format The final exam is 120-minutes, closed- book,
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.
Agenda  Perform Quiz#3 (15 Minutes)  Introduction to Pointers –What are pointers? / Why use pointers? –Pointer Terminology Memory Address format specifier.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Week 6: Functions - Part 2 BJ Furman 01OCT2012. The Plan for Today Comments on midterm exam (next week in lab!) Review of functions Scope of identifiers.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the concept and use of pointers ❏ To be able to declare, define,
1 2/2/05CS250 Introduction to Computer Science II Pointers.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
LINKED LISTS.
Lesson #8 Structures Linked Lists Command Line Arguments.
Introduction to Programming Using C
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
EPSII 59:006 Spring 2004.
C Language By Sra Sontisirikit
Quiz 11/15/16 – C functions, arrays and strings
Pointers.
INC 161 , CPE 100 Computer Programming
Lesson #6 Modular Programming and Functions.
Functions Declarations CSCI 230
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
2008/11/24: Lecture 19 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Pointers  Week 10.
بنام خدا زبان برنامه نویسی C (21814( Lecture 11 Pointers
IPC144 Week 10 – Lesson 2 Working with Files
Functions.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Functions with arrays.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Pointers.
Lesson #6 Modular Programming and Functions.
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Week 9 – Lesson 2 Input & Output of Character Strings
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Arrays.
Pointers Chapter 11 Copyright © 2008 W. W. Norton & Company.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Functions I Creating a programming with small logical units of code.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
FUNCTION ||.
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
CPS125.
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Introduction to Pointers
Presentation transcript:

IPC144 Introduction to Programming Using C Week 6 – Lesson 1 (Pages 47 to 51 in IPC144 Textbook)

Agenda Additional C programming Elements Pointers Purpose Passing Pointers to Functions Indirection Example

Modularity We have learned in previous lessons, that functions allow us to break-down programs into smaller elements. We can pass parameters to functions, and have a value returned from a function… There is a problem: Functions can only return one value, not multiple values…

Pointers Pointers are a method or “trick” to be able to pass up the memory address of variables, and allow the user to change the value of the variable via its memory address. Memory address of variables Function Values of variables changed in main ( ) for example…. Change values of variables within Function (Through a process called indirection …)

Pointers First, the function header and prototype should indicate which parameter(s) are a “pointer” or “memory address” to variable. This is accomplished by placing an asterisk before the variable… eg. void functionName (double x, int *y) Indicates a “cast” i.e. parameter is accepting a “Pointer” or memory address location of variable called y ….

Pointers Second, when the function that accepts pointers is called, you must include an “&” symbol before the variable name that is going to represent the pointer or “memory address” to that variable Using the previous slide’s example, being called with main (): functionName (x, &y); Note: second variable used as a pointer. Just using the same names in main() for variables to prevent confusion…

Pointers Third, within the function, to change the original value of the variable (passed up as a parameter to your function as a pointer), you use indirection. You simply place an asterisk in front of the variable to make reference to it’s original value. Using the previous slide’s example, now within the function called “functionName”: *y = *y + x; For example: original y in main is equal to original value of y + x

Example #include <stdio.h> void hours_and_minutes(int total, int *phrs, int *pmin); main() { int tmin, hr, min; printf (“Enter total minutes: “); scanf (“%d”, &tmin); hours_and_minutes(tmin, &hr, &min); printf (“%d hours, and %d minutes\n”, hr, min); } void hours_and_minutes(int total, int *phrs, int *pmin) *pmin = total % 60; *phrs = total / 60;

Homework TASK #1 TASK #2 TASK #4 *** Highly Recommended *** Complete lab #5 since it is due at the beginning of this week’s lab! TASK #2 Study for test #1 – to be held in week #7 TASK #4 *** Highly Recommended *** Read ahead in IPC144 Programming Notes (Filling in the Gaps).