Modularity using Functions Chapter 4. Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code.

Slides:



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

Introduction to C Programming
Basic Logic – Chapter 3 IPC144 Week 3. Sequential Logic Statements executed sequentially – one after another Limited usefulness All programs shown so.
C Language.
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Spring Semester 2013 Lecture 5
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 ICS103 Programming in C Lecture 3: Introduction to C (2)
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
CS 201 Functions Debzani Deb.
Introduction to Methods
Chapter 6: Functions.
Apply Sub Procedures/Methods and User Defined Functions
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Agenda  Perform Quiz#2 (20 Minutes)  Functions / Continued … –Functions - Definition –Types of Functions: Functions that do not accept or return a value.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Fundamental Programming: Fundamental Programming Introduction to C++
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
NA2204.1jcmt CSE 1320 Intermediate Programming C Program Basics Structure of a program and a function type name (parameters) { /* declarations */ statement;
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
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.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
Chapter 3: User-Defined Functions I
Agenda  Take up homework  Loops - Continued –For loops Structure / Example involving a for loop  Storing Characters in variables  Introduction to Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
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.
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.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 9: Value-Returning Functions
Chapter 7: Function.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Lesson #6 Modular Programming and Functions.
An Introduction to Programming with C++ Sixth Edition
Lesson #6 Modular Programming and Functions.
ICS103 Programming in C Lecture 3: Introduction to C (2)
Looping.
Functions Declarations CSCI 230
Functions I Creating a programming with small logical units of code.
Functions.
A function with one argument
Lesson #6 Modular Programming and Functions.
Functions Imran Rashid CTO at ManiWeber Technologies.
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

Modularity using Functions Chapter 4

Modularity In programming blocks of code often can be "called up" and reused whenever necessary, for example code to calculate GST and PST, or code to validate inputs or code to convert values (eg from miles to kilometers). A block of reusable code placed in a separate unit is called a function. This reusable code can then be called from another function. main is actually a function itself. Programs become more modular and are easier to maintain through the use of functions.

/* Program to input values between 0-20 for number of large, medium and small coffees sold and calculate total cost of coffees. */ main () { int large, medium, small; double cost; printf ("Enter number of large coffee (0-20): "); scanf ("%d", &large); while (large 20) {printf ("0-20 only. Please try again\n"); scanf ("%d", &large);} printf ("Enter number of medium coffee (0-20): "); scanf ("%d", &medium); while (medium 20) {printf ("0-20 only. Please try again\n"); scanf ("%d", &medium);} printf ("Enter number of small coffee (0-20): "); scanf ("%d", &small); while (small 20) {printf ("0-20 only. Please try again\n"); scanf ("%d", &small); } cost = (large * 1.80) + (medium * 1.50) + (small * 1.10); printf ("The cost for coffee is: $%.2lf\n", cost);}

int GetNumber (char size); /* function prototype */ main () {int large, medium, small; double cost; large = GetNumber('L'); /* function call */ medium = GetNumber('M'); /* function call */ small = GetNumber('S'); /* function call */ cost = (large * 1.80) + (medium * 1.50) + (small * 1.10); printf ("The cost for coffee is: $%.2lf\n", cost); } int GetNumber (char size) /* function heading */ {int num; /* local variable */ printf ("Enter number of %c coffee (0-20): ", size); scanf ("%d", &num); while (num 20) {printf ("0-20 only. Try again. # of %c coffee:\n",size); scanf ("%d", &num); } return num; /* return num to main */ } /* Program to input values between 0-20 for number of large, medium and small coffees sold and calculate total cost of coffees. Uses function called GetNumber to validate input is between 0-20 */

Benefits of Modularity Less coding required  fewer syntax and logic errors Easier to test Easier to understand Function can be reused by other programs

Functions There are 4 elements of a function: function heading function body function call function prototype

Function Heading A function heading defines three things: 1)the type of value to be returned from the function 2)the name of the function (the same rules apply here as for naming variables) 3)the parameter list (the type and name of the values to be passed into the function) Example: int GetNumber (char size)

Function Body The function body is placed just after the function heading. Function body is enclosed in brace brackets and contains the code for the function task. A function may declare some local variables. A function may return only a single value of the type defined in the function heading. Example: int num; /* local variable */ printf ("Enter number of %c coffee (0-20): ", size); scanf ("%d", &num); while (num 20) {printf ("0-20 only. Please try again\n"); scanf ("%d", &num); } return num;

Function Call A function can be called from main or from another function. To call a function, use the name of the function and a list of arguments (separated by commas) that are being sent to the function. The arguments in the function call must match the data type and order of the arguments shown in the parameter list of the function heading. If the function returns a value which you want to store, then you would use an assignment statement when calling the function.For example: large = GetNumber('L');

Function Prototype Functions may be written above main but most programs place all functions below main so it is easy to find the first line that will be executed in the program (execution always begins with the first line in main). When functions are placed below main, a function prototype must appear above main. The function prototype allows C to find and report any illegal data type conversions (for example, sending a double to a function where an integer is expected). Think of the function prototype as the function declaration Example: int GetNumber (char size);

Modularity Guidelines Each function should perform a single task (for example: calculate the volume, or ask the user to input a mark, or print an invoice) and should be given a name that reflects that task (for example: CalcVolume or GetMark or PrintInvoice) Each function should be a manageable length (one page or one screen in length as a guide). Note: this also includes the main function. The number of parameters in a function heading should be reasonable (5 to 10 as a guide) If one function is too long, too complex, or has too many parameters, perhaps you need to subdivide it into several functions.