U NIT 7 Functions and Modular Programming Introduction to C Programming 1.

Slides:



Advertisements
Similar presentations
Topic Reviews For Unit ET156 – Introduction to C Programming Topic Reviews For Unit
Advertisements

TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
UNIT 9: Pointers Data Variable and Pointer Variable Pass by Reference
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Introduction to C Programming
Introduction to C Programming
Credit hours: 4 Contact hours: 50 (30 Theory, 20 Lab) Prerequisite: TB143 Introduction to Personal Computers.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 6: Modular Programming Problem Solving & Program Design in.
0 - 0.
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULTIPLICATION EQUATIONS 1. SOLVE FOR X 3. WHAT EVER YOU DO TO ONE SIDE YOU HAVE TO DO TO THE OTHER 2. DIVIDE BY THE NUMBER IN FRONT OF THE VARIABLE.
SUBTRACTING INTEGERS 1. CHANGE THE SUBTRACTION SIGN TO ADDITION
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Introduction to Programming
Pointer Lesson 2 CS1313 Spring Pointer Lesson 2 Outline 1.Pointer Lesson 2 Outline 2.Pass by Reference Bad Example 3.Pass by Reference Good Example.
Chapter 7: Arrays In this chapter, you will learn about
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
StructuresStructures Systems Programming. Systems Programming: Structures 2 Systems Programming: 2 StructuresStructures Structures Structures Typedef.
StructuresStructures Systems Programming. StructuresStructures Structures Structures Typedef Typedef Declarations Declarations Using Structures with Functions.
ABC Technology Project
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Structures –Collections of related.
1 Pointers and Strings Section 5.4, , Lecture 12.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
1 Structures. 2 Structure Basics A structure is a collection of data values, called data members, that form a single unit. Unlike arrays, the data members.
User Defined Functions
1 Chapter Eleven Arrays. 2 A Motivating Example main( ) { int n0, n1, n2, n3, n4; scanf(“%d”, &n0); scanf(“%d”, &n1); scanf(“%d”, &n2); scanf(“%d”, &n3);
Created by Susan Neal $100 Fractions Addition Fractions Subtraction Fractions Multiplication Fractions Division General $200 $300 $400 $500 $100 $200.
1 Advanced C Programming from Expert C Programming: Deep C Secrets by Peter van der Linden CIS*2450 Advanced Programming Concepts.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Past Tense Probe. Past Tense Probe Past Tense Probe – Practice 1.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
Addition 1’s to 20.
25 seconds left…...
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Test B, 100 Subtraction Facts
11 = This is the fact family. You say: 8+3=11 and 3+8=11
Week 1.
Pointers and Arrays Chapter 12
Senem KUMOVA METİN CS FALL 1 POINTERS && ARRAYS CHAPTER 6.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
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.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 6: Functions.
CP104 Introduction to Programming Modular Programming Lecture 16__ 1 Modular Programming II Functions with single output Functions with multiple outputs.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
1 ICS103 Programming in C Ch3: Top-Down Design with Functions.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
User-Written Functions
Introduction to Programming
Deitel- C:How to Program (5ed)
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
© A+ Computer Science - OOP Pieces © A+ Computer Science -
IPC144 Introduction to Programming Using C Week 6 – Lesson 1
Arrays.
Chapter 6 Modular Programming chap6.
Presentation transcript:

U NIT 7 Functions and Modular Programming Introduction to C Programming 1

U NIT 7 KEY CONCEPTS 1. Identifier scope 2. Pointers 3. Function prototypes and function bodies 4. Input parameters 5. Output parameters 6. Multiple functions 7. Types of testing 2

S COPES What is a file scope? What is a block scope? What is a local scope? 3

4 S COPE OF N AMES Identifiers (variables and functions) defined in the main function can be accessed anywhere within the program Unless the same name is used within a function or block Identifiers (variables and functions) defined in a function can only be accessed within that function Identifiers (variables and functions) defined in a block can only be accessed within that block

5 //Demo 1 #include int main(void){ int x = 0; printf("This x equals %d\n", x); { int x=10; x += 10; printf(" This x equals %d\n", x); } printf(" The original x equals %d\n", x); return(0);}

P OINTERS A pointer is a variable that holds the address of another variable. It is an advanced concept, however for our purposes we will use it with certain functions. Two new operators & is the Address of operator * is the Direct Reference operator (sometimes called the dereference operator) Examples 6

P OINTERS Pointers allow us to directly access a variable at its memory location. This allows us to by-pass scoping limitations. 7

P OINTERS int i = 5; is a integer variable equal to 5 int *p_i; is a pointer, reads as pointer to I p_i = &i; assigns the address of i to the pointer of i *p_i Reads as the direct reference to i This value is five 8

9 F IGURE 3.6 F UNCTION SQRT AS A B LACK B OX

10 F UNCTION TYPES 10 Return Type Purpose # Input Parameters # Output Parameters Return Result Defined TypeTo return a single value0+1Same as type voidTo produce a printed output0+0None voidTo compute multiple results0+2+ Pointers voidTo modify input arguments1+none Pointers

11 F UNCTIONS WITH I NPUT P ARAMETERS Data is passed into the function, but cannot be modified by the function. int main(void) { double calcFr(double L, double C) ; //prototype double C1, L1, resonantfreq; C1 = 47E-6; L1 = 100E-3 resonantfreq = calcFr (C1, L1); //invocation return 0;} double calcFr(double L, double C) { // definition double Fr; Fr = 1 / (2*PI*sqrt(L*C)); return (Fr); }

12 F IGURE 3.20 F UNCTION WITH I NPUT A RGUMENTS AND O NE R ESULT

13 F UNCTION WITH M ULTIPLE R ESULTS 13 Use the address of operator (&) before the name of the variable. Example: void calcVoltageCurrent(double W, int R, double *volts, *amps); calcVoltageCurrent(W, R, &volts, &amps); void calcVoltageCurrent(double W, int R, double *volts, *amps){ *volts = sqrt(W * R); *amps = sqrt(W / R); }

F UNCTION WITH M ULTIPLE R ESULTS int main(void){ void calcVoltageCurrent(double W, int R, double *V, double *I); double power =.1, volts, amps; int load = 1000; calcVoltageCurrent(power, load, &volts, &amps); printf("V is %f and \n I is %f\n", volts, amps); return 0;} void calcVoltageCurrent(double W, int R, double *V, double *I){ *V = sqrt(W * R); *I = sqrt(W / R); } 14

15 separate( value, &sn, &whl, &fr); 15

16 F IGURE 6.11 S TRUCTURE C HART FOR C OMMON F RACTION P ROBLEM

T OP -D OWN VS. B OTTOM -U P T ESTING 17 Create function stubs to test the main program. Add each function as it is complete and retest. Separately test each function before integration. Create a driver to test the function. Unit testing – testing a function Integration testing – testing the complete program after all functions have been added Top-down testingBottom-up testing