How to design and code functions Chapter 4 (ctd).

Slides:



Advertisements
Similar presentations
Functions Prototypes, parameter passing, return values, activation frams.
Advertisements

Lab 8 User Defined Function.
Exercise 4 1. Write a program that simulates coin tossing. For each toss of the coin the program should print Heads or Tails. Let the program toss the.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Software Development Method. Assignments Due – Homework 0, Warmup Reading – Chapter 2 –
Array What it is. How to use it How to declare it How to initialize it.
1 Passing Array Array’s element can be passed individually to a function; copying value exist during passing process. An entire array can be passed to.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Lecture 6 – Functions (2). Outline Recall - sample application functions that return no value functions that return a value Recall – global variable vs.
1. Function prototype Function prototype is a declaration; indicates the function exists Should have function name, return type and parameter Placed before.
Overview of Programming and Problem Solving Textbook Chapter 1 1.
How to start Visual Studio 2008 or 2010 (command-line program)
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
1 C Programming Week 2 Variables, flow control and the Debugger.
UniMAP SemII-09/10EKT120: Computer Programming1 Week 6 – Functions (2)
A First Book of ANSI C, Fourth Edition1 Functions for Modularity 04/24/15.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
To find time and a half rates & double time rates based on and hourly wage 1. 5 ½ =.5 = 5/10 Hourly rate X 1.5 Overtime rate 2 Hourly rate X 2__ Overtime.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
UniMAP Sem2-09/10 DKT121:Fundamental of Computer Programming1 Functions (2)
/* example program to demonstrate the passing of an array */ #include int maximum( int [] ); /* ANSI function prototype */ int maximum(
Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
NOTE: C programs consist of functions one of which must be main. C programs consist of functions one of which must be main. Every C program begins executing.
The Repetition control structure using while loop.
EKT120: Computer Programming
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Formatted Input/Output
Week 4 – Repetition Structures / Loops
Week 4 – Chapter 3 Repetition.
PGT 106: Computer Programming
Week 5 – Functions (1) EKT120: Computer Programming.
EKT120: Computer Programming
DKT121:Fundamental of Computer Programming
Functions in C Mrs. Chitra M. Gaikwad.
Formatted Input/Output
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
Chapter 8 The Loops By: Mr. Baha Hanene.
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
The while Looping Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
A function with one argument
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Example Using Functions
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Solutions to In-Class Problems
In C Programming Language
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
The while Looping Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
EECE.2160 ECE Application Programming
CSCE 206 Lab Structured Programming in C
Functions, Part 2 of 3 Topics Functions That Return a Value
The while Looping Structure
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Functions, Part 2 of 3 Topics Functions That Return a Value
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Week 3 – Repetition (ctd.)
Arrays, Part 2 of 2 Topics Array Names Hold Address How Indexing Works
Presentation transcript:

How to design and code functions Chapter 4 (ctd)

Functions Example Write a program using functions to calculate weekly pay for an employee Inputs include: number of hours worked (assume only whole numbers) and hourly wage for the employee. Weekly pay to be calculated using overtime policy Output: hours worked, hourly wage and weekly pay

Identify tasks Identify separate tasks to be performed by program. Program should be coded using a function with a meaningful name for each task as follows: ask user to enter number of hours worked  getHours ask user to enter hourly wage  getRate calculate weekly pay using overtime policy  calculatePay print hours worked, hourly wage, weekly pay  printResults

Identify values to be passed to function (input parameters) Next step is to identify values required (i.e. input parameters) by each function as follows: getHours: no values to be passed  getHours() getRate: no values to be passed  getRate() calculatePay: to be passed hours and rate  calculatePay(int hours, float rate) printResults: to be passed hours, rate and pay  printResults(int hours, float rate, float pay)

Identify value to be returned by function (return value) Next step is to identify value to be returned by each function as follows: getHours: return hours worked  int getHours() getRate: return hourly rate  float getRate() calculatePay: return weekly pay  float calculatePay(int hours, float rate) printResults: no values to be returned  void printResults(int hours, float rate, float pay)

Identify value to be returned by function (return value) Next step is to identify value to be returned by each function as follows: getHours: return hours worked  int getHours() getRate: return hourly rate  float getRate() calculatePay: return weekly pay  float calculatePay(int hours, float rate) printResults: no values to be returned  void printResults(int hours, float rate, float pay) Use these as your function prototypes

Code each function Function getHours inputs and returns an integer value for hours worked (between 0 and 60): int getHours() { int hours; do { printf("Enter hours worked: "); scanf("%d", &hours); if (hours 60) printf("Invalid hours (0-60)\n"); } while (hours 60); return hours; }

Code each function (ctd) Function getRate inputs and returns a float value for hourly rate (between and 20): float getRate(){ float rate; do { printf("Enter hourly rate: "); scanf("%f", &rate); if (rate 20.00) printf("Invalid rate ( )\n"); } while (rate 20); return rate; }

Code each function (ctd) Function calculatePay calculates weekly pay paying hourly rate for first 40 hours and time and a half for overtime hours: float calculatePay(int hours, float rate) {float pay; if (hours > 40) pay = 40*hours * rate + (hours-40)*1.5 * rate ; else pay = hours * rate ; return pay ; }

Code each function(ctd) Function printResults displays hourly rate, hours worked and pay: void printResults(int hours, float rate, float pay) { printf ("*** Weekly Pay ***\n") ; printf ("*****************\n") ; printf ("Hours worked: %d\n", hours) ; printf ("Hourly rate: $%.2lf\n", rate) ; printf ("Pay: $%.2lf\n", pay) ; }

Code main function Function main calls getHours, getRate, calculatePay and printResults: void main () { int hours ; float rate, pay ; hours = getHours ( ) ; rate = getRate ( ) ; pay = calculatePay (hours, rate) ; printResults (hours, rate, pay) ; }

Assemble and test program Assemble program by adding functions to main (start with input functions) and testing thoroughly: int getHours(); void main () { int hours ; float rate, pay ; hours = getHours ( ) ; printf(“Hours: %d\n”,hours); /* for testing only */ } int getHours() {int hrs; do { printf(“Enter hours worked: “); scanf(“%d”, &hrs); if (hrs 60) printf(“Invalid hrs (0-60)\n”); } while (hours 60); return hrs; }

Assemble and test program (ctd) Next add getRate to program and test: int getHours(); float getRate(); void main () { … } int getHours() {… } float getRate() {… }

Assemble and test program (ctd) Next add calculatePay to program and test: int getHours(); float getRate(); float calculatePay(int hours, float rate); void main () { … } int getHours() {… } float getRate() {… } float calculatePay(int hours, float rate) {… }

Assemble and test program (ctd) Now add printResults to program and test: int getHours(); float getRate(); float calculatePay(int hours, float rate); void printResults(int hours, float rate, float pay); void main () { … } int getHours() {… } float getRate() {… } float calculatePay(int hours, float rate) {… } void printResults(int hours, float rate, float pay) {… }