Presentation is loading. Please wait.

Presentation is loading. Please wait.

IPC144 Introduction to Programming Using C Week 5 – Lesson 1

Similar presentations


Presentation on theme: "IPC144 Introduction to Programming Using C Week 5 – Lesson 1"— Presentation transcript:

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

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

3 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)…

4 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;

5 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…

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

7 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()

8 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

9 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!

10 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…

11 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 #define PST main() { etc …

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

13 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).


Download ppt "IPC144 Introduction to Programming Using C Week 5 – Lesson 1"

Similar presentations


Ads by Google