Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Lecture 3 Strings More control statements C functions.
Chapter 6: User-Defined Functions I
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Functions in C Outline 1Introduction 2Program Modules in C 3Math Library Functions 4Functions 5Function Definitions 6Function Prototypes 7Header Files.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
Chapter 5 - Functions Outline 5.1Introduction 5.2Program Modules in C 5.3Math Library Functions 5.4Functions 5.5Function Definitions 5.6Function Prototypes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
Sudeshna Sarkar, IIT Kharagpur 1 Functions Lecture
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Lecture 7: Modular Programming (functions) B Burlingame 05 October, 2016.
Functions Course conducted by: Md.Raihan ul Masood
User-Written Functions
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Lecture 7: Modular Programming (functions)
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 5 - Functions Outline 5.1 Introduction
Functions.
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Chapter 5 - Functions Outline 5.1 Introduction
Lesson #6 Modular Programming and Functions.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions Declarations CSCI 230
Chapter 6 - Functions Outline 5.1 Introduction
Functions, Part 1 of 3 Topics Using Predefined Functions
Lesson #6 Modular Programming and Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
In C Programming Language
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
CPS125.
Presentation transcript:

Lecture 5: Modular Programming (functions – part 1 BJ Furman 27FEB2012

Learning Objectives Explain the concept of modular program design Explain the concept of a function in C Explain why functions are important in programming Explain the structure of a function  Return data type  Parameters Apply the concept of a function to a practical problem Explain how larger C programs should be structured using.h and.c files

Observations so far Starting to get the hang of it!  Don’t give up if you are still fuzzy on some of the concepts! See Prof. Furman or lab instructor. Don’t slack off on the steps in the process of developing a program:  Define the problem  Determine requirements/specifications  Define inputs and outputs  Develop the ‘logic’ of the program using pseudocode and/or a flowchart  Develop the C code (including comments!!!)  Test the program (consider full range of possible inputs)

Modular Programming Break a large problem into smaller pieces  Smaller pieces sometimes called ‘modules’ or ‘subroutines’ or ‘procedures’ or functions  Why? Helps manage complexity  Smaller blocks of code  Easier to read Encourages re-use of code  Within a particular program or across different programs Allows independent development of code Provides a layer of ‘abstraction’ a = sqrt(9.0);

Functions The ‘building blocks’ of a C program  You’ve used predefined functions already: main() printf(), scanf(), pow()  User-defined functions Your own code In combination with predefined functions

Functions - Mathematical View X Function Returned value

Functions - Definition Structure Function 'header'  Return data type (if any)  Name Descriptive  Arguments (or parameter list) Notice: data type and name Statements  Variable declaration  Operations  Return value (if any) type function_name (type arg1, type arg2 ) { statements; } double product(double x, double y) { double result; result = x * y; return result; } A function that calculates the product of two numbers

Functions - Example Function prototype  Like a variable declaration Tells compiler that the function will be defined later Helps detect program errors Note semicolon!! Function definition  See previous slide  Note, NO semicolon Function return  return statement terminates execution of the current function  Control returns to the calling function  if return expression; then value of expression is returned as the value of the function call Only one value can be returned this way Function call  main() is the 'calling function'  product() is the 'called function'  Control transferred to the function code  Code in function definition is executed #include /* function prototype */ double product(double x, double y); int main() { double var1 = 3.0, var2 = 5.0; double ans; ans = product(var1, var2); printf("var1 = %.2f\n" "var2 = %.2f\n",var1,var2); printf("var1*var2 = %g\n", ans); } /* function definition */ double product(double x, double y) { double result; result = x * y; return result; }

Function - Practice 1 Write a function named 'sum'  sums two integers  returns the sum 2 min. on your own Share with neighbor Steps 1.Function header return data type function name argument list with data types 2.Statements in function definition variable declaration operations return value

Function - sum() int sum_int(int x, int y) { int result; result = x + y; return result; }

Functions that do not return a value Use the return type of void  void my_fun( arg_list,…) Practice  Write two functions, the first prints out first name, and the second prints out last name

Function - Practice 2 Program to print out two happy :) :) or sad faces :( :(  Continuously prompts for user input: ) for happy face ( for sad face Quits if 'q' or 'Q' entered  calls two functions happy_face() sad_face() Work in pairs Pseudocode first!! Divide tasks of writing the two functions Steps 1.Pseudocode for program logic 2.Function header return data type (if any) function name argument list with data types (if any) 3.Statements in function definition variable declaration (if any) operations return value

Program - Faces logic Pseudocode 1. Declare and initialize variables 2. WHILE user input not equal to q AND not equal to Q 1) Switch on user input to 2) Case ')‘: call happy_face(); break; 5) Case '(‘: call sad_face(); break; 6) Case ‘q’: 7) Case ‘Q’: break; 8) Case ‘0’: 9) Default: re-prompt for user input

Program - Faces code

Structuring C Programs Modularization  Breaking a program up into smaller pieces: Instead of:  one_big_program.c  break into groupings of header files (.h) and source code (.c) files:  module_1.h  module_1.c  etc. Rationale  separates the user-interface description (.h) from the nitty- gritty details of implementation (.c) The Application Programming Interface (API), the.h file, is distinct from the implementation, the.c file (which may already be compiled and not readily viewed) Example: math.h from Ch  can construct and test modules independently  promotes re-use of code

Example: math.h used in Ch See C:/ Ch / include / math.h  Declaration of constants #define M_PI  Declaration of macro subsitutions #define isgreater(x, y) ((x)>(y))  Declaration of global variables (caution!)  Function prototypes extern double sin(double x);  Pertinent comments

Review

Structured Programming All programs can be written using these control structures:  Sequence  Decision (three structures) IF IF-ELSE SWITCH  Repetition (three structures) WHILE DO-WHILE FOR

Structure of a C program Ex. free_fall_d_vs_time.c

C Code for D&D 3.15c Programmer’s block Pre-processor directive Declare and initialize variables While loop (repetition structure) Main function (statements go between { } ) return statement

Arithmetic with Integers and Mixed Data Types Arithmetic with integers  Result is an integer > 2 4/2 --> 2 2/4 --> ?BE CAREFUL!!! Arithmetic with mixed data types  Automatic conversion of operand so that data types match Conversion is ‘upward’ in the sizeof() sense Example in Ch char a = 7; sizeof(a); double b=3; sizeof(b); printf("a+b == %lf and needs %d bytes\n ", a+b, sizeof(a+b));

References Modular Programming in C math.h /xsh/math.h.html /xsh/math.h.html