IPC144 Introduction to Programming Using C Week 4 – Lesson 2

Slides:



Advertisements
Similar presentations
Intro Programming By Trevor Decker Team 1743 By Trevor Decker Team 1743.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Java Unit 9: Arrays Declaring and Processing Arrays.
Python quick start guide
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
CPS120: Introduction to Computer Science Decision Making in Programs.
142 F -1 Functions chapter 3 of the text int main(void) { double x,y,z; … x = cube(y/3.0); … printf(“%f cubed is %f”,x,cube(x)); … return 0; } double cube(double.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CPS120: Introduction to Computer Science Lecture 14 Functions.
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.
Building Java Programs Parameters and Objects. 2 Redundant recipes Recipe for baking 20 cookies: –Mix the following ingredients in a bowl: 4 cups flour.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
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.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lesson #6 Modular Programming and Functions.
Introduction To Repetition The for loop
Lesson #6 Modular Programming and Functions.
Suppose we want to print out the word MISSISSIPPI in big letters.
Functions CIS 40 – Introduction to Programming in Python
Chapter 4: Writing Classes
Counted Loops.
Lesson #6 Modular Programming and Functions.
CS 106A, Lecture 7 Parameters and Return
Arrays & Functions Lesson xx
IPC144 Introduction to Programming Using C Week 2 – Lesson 1
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Conditions and Ifs BIS1523 – Lecture 8.
Introduction to Programming
How to Run a Java Program
IPC144 Week 10 – Lesson 2 Working with Files
IPC144 Introduction to Programming Using C Week 3 – Lesson 1
While Loops and If-Else Structures
IPC144 Introduction to Programming Using C Week 3 – Lesson 2
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Lec 4: while loop and do-while loop
Intro to Computer Science CS1510 Dr. Sarah Diesburg
While Loops and If-Else Structures
While Loops and If-Else Structures
While Loops and If-Else Structures
Lesson #6 Modular Programming and Functions.
Week 4 Lecture-2 Chapter 6 (Methods).
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
IPC144 Introduction to Programming Using C Week 4 – Lesson 1
While Loops and If-Else Structures
EECE.2160 ECE Application Programming
Introduction to Repetition
Looping and Multiple Forms TEST REVIEW
Agenda Warmup Lesson 2.2 (parameters, etc)
Introduction to Repetition
While Loops and If-Else Structures
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
While Loops And If-Else Structures
IPC144 Introduction to Programming Using C Week 2 – Lesson 2
Lecture 20 – Practice Exercises 4
Presentation transcript:

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

Agenda Additional C programming Elements Modularity / Continued… Creating a Simple Function Sending Parameters to a Function Returning values from a Function Examples

Modularity User-defined Functions In the previous lesson slides, we used a function that is simply run like a program when executed as a statement within our main program. The purpose of functions is to create a smaller program that can be used over-and-over again in the main() program to save coding and reduce the time required to create programs. In the REALITY CHECK Question #3 (Week 4 – Lesson 1), we created a function to print the title. Could we use the function more than once?

Modularity The answer to the previous slide’s question is “NO”. Not in its current form – it is only designed to print the title at the top, or print the footer at the bottom. We could become efficient, and just use one function to generate a header and a footer, but it would require that we send a value (or values) to the function to use…

Modularity For example, we could define a function called drawBox() that accepts text inside the function. When the drawBox() function executes, it prints a line, on the next line, prints the text, then on the last line, prints another line Here is an example: drawBox(“Title of Report”); ----------------------- Title of Report -----------------------

Modularity Instead of putting text inside function to pass- up to function when it executes, we can use variables instead! But remember: when Programming in C, we need to first DEFINE the data-type of the variable… How is this done with functions?

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 We can’t use text for our function example, but we can indicate how to draw our box… For example: number of columns, and number of rows… e.g. drawBox (5, 3); ***** ***** *****

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

Modularity How could we improve Question #2 to error- check the values of number of rows and number of columns to make certain they are in appropriate boundaries (eg. between 1 and 24?) We can define a function to return a value (for example an int 1 for TRUE and 0 for FALSE). Based on calling the function, the program can loop until the data is correct…

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;

Practice REALITY CHECK (Week 4 – Lesson 2) Question #3 (Word Problem)

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…

Homework TASK #1 TASK #2 TASK #3 TASK #4 *** Highly Recommended *** Complete lab #3 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 (Modularity / Functions).