IPC144 Introduction to Programming Using C Week 5 – Lesson 1

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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.
Week 4 – Functions Introduction. Functions: Purpose Breaking a large problem into a series of smaller problems is a common problem- solving technique.
Week 8 Arrays Part 2 String & Pointer
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.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.
Agenda  Perform Quiz#3 (15 Minutes)  Introduction to Pointers –What are pointers? / Why use pointers? –Pointer Terminology Memory Address format specifier.
CMSC 1041 Functions II Functions that return a value.
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.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
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.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
UNIT 5 C Pointers.
Functions, Part 2 of 2 Topics Functions That Return a Value
EPSII 59:006 Spring 2004.
C Language By Sra Sontisirikit
CS1010 Discussion Group 11 Week 9 – Pointers.
Quiz 11/15/16 – C functions, arrays and strings
CS1010 Programming Methodology
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Functions I Creating a programming with small logical units of code.
Pointers  Week 10.
Week 9 – Lesson 1 Arrays – Character Strings
IPC144 Week 10 – Lesson 2 Working with Files
Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
Defining methods and more arrays
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
A function with one argument
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Functions with arrays.
Function In this lesson, you will learn about Introduction to Function
Functions, Part 1 of 3 Topics Using Predefined Functions
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
Functions continued.
Week 9 – Lesson 2 Input & Output of Character Strings
Fundamental Programming
EECE.2160 ECE Application Programming
Functions Extra Examples.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Programming in C Pointers and Arrays.
C++ Array 1.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 42 Topics Functions That Return a Value
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
ITM 352 Functions.
Functions, Part 2 of 3 Topics Functions That Return a Value
CPS125.
Functions that return a value
Scope Rules.
Presentation transcript:

IPC144 Introduction to Programming Using C Week 5 – Lesson 1 (Pages 38 to 46 in IPC144 Textbook)

Agenda Additional C programming Elements Modularity / Continued… Additional Examples of Functions Passing up parameters and returning values GLOBAL vs Local variables

Modularity In the function header and the function prototype, we specify the data-type in the brackets. First, the data-type declaration statement, then the variable name that the function will use. For example: void drawBox(int x); We will be learning later in this course how to work with characters and text (character strings)…

Modularity If you are having functions return a value: You need to specify the data-type of the function that is returning a value in the function header and function prototype. eg. int checkData(); You must somewhere within the function use the return statement to pass-back a value to the main() program (can be within a variable name)… eg return 0; or return x;

Modularity Of course, functions can accept and return double data-types as well… Later in this course, we will learn other data-types such as characters, which can be used for a function to accept and/or return as well…

Practice REALITY CHECK (Week 5 – Lesson 1) Question #1 (Word Problem) Question #2 (Walk-Thru) Question #3 (Walk-Thru)

Local Variables From performing the previous walk-thru (Question #3 in REALITY CHECK W5L1), you should have noticed each time a variable is declared and used within the function, that variable is only used within that function. These type of variables are called Local Variables which are only defined within a function or main()

GLOBAL Variables Another type of variable is called a GLOBAL variable which means that the variable can be used by any of the function without passing up its value to the function. GLOBAL variables are defined outside of functions or main before the main() code block

GLOBAL Variables For Example: #include <stdio.h> int NUM = 1; void function_1 (void); int main (void) { printf ("NUM = %d\n", NUM); function_1(); return 0; } void function_1 (void) NUM = NUM + 5; The integer NUM is declared and assigned value outside main or function code blocks The GLOBAL variable is changed in the function and thus main program as well without passing values to function or having values returned… This is dangerous!

GLOBAL Variables It is dangerous to use GLOBAL variables since many functions that are created by many different people for a C program may not be aware of that GLOBAL variable. It is better to use functions to pass-up variables to be used locally, and return values…

GLOBAL Variables Therefore, GLOBAL variables should not be used if intended to have their values changed within a running program. On the other hand, GLOBAL variables can be used as constants (i.e. values that are not changed)… You can use the #define statement (below the #include statement to set the value of a CONSTANT) #include<stdio.h> #define GST 0.05 #define PST 0.08 main() { etc …

Practice REALITY CHECK (Week 5 – Lesson 1) Question #4 (Word Problem)

Homework TASK #1 TASK #2 TASK #3 TASK #4 *** Highly Recommended *** Complete lab #4 since it is due at the beginning of this week’s lab! TASK #2 Study for a quiz to be held in this week’s lab based on material taught last week TASK #3 Complete and code the solutions for today’s REALITY CHECK QUESTIONS, as well as “hide” and repeat the walk-thru questions TASK #4 *** Highly Recommended *** Read ahead in IPC144 Programming Notes (Pointers).