1 CSC103: Introduction to Computer and Programming Lecture No 13.

Slides:



Advertisements
Similar presentations
1 ICS103 Programming in C Lecture 5: Introduction to Functions.
Advertisements

Sequence Diagrams. Introduction A Sequence diagram depicts the sequence of actions that occur in a system. The invocation of methods in each object, and.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
January Ron McFadyen1 Ch 9. Use-case model: drawing System Sequence Diagrams Elaboration Iteration 1: a simple cash-only success scenario of.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Unit 211 Requirements Phase The objective of this section is to introduce software system requirements and to explain different ways of expressing these.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2007 Pearson Education, Inc. All rights reserved C Functions.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Object Oriented Software Development
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
You gotta be cool. Introduction to Classes, Objects and Strings Introduction Defining a Class with a Member Function Defining a Member Function with a.
CS212: Object Oriented Analysis and Design Lecture 4: Objects and Classes - I.
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.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Parent Portal Also known as: The next best thing to being at school with your student!
1 CSC103: Introduction to Computer and Programming Lecture No 11.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Chapter 3, Section 3 ELECTRONIC BANKING.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Chapter 4 Introduction to Classes, Objects, Methods and strings
1 CS161 Introduction to Computer Science Topic #9.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
1 Graph Coverage (6). Reading Assignment P. Ammann and J. Offutt “Introduction to Software Testing” ◦ Section
Introduction to Business Analytics & Business Intelligence Information Systems Functions i-Clicker Demo IS vs IT IPO Model Note Taking.
Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
H1-1 University of Washington Computer Programming I Lecture 9: Iteration © 2000 UW CSE.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Topic sentence--2. People need to get a bank card if they want to use an ATM. bank card In order to apply, you must fill out an application. Once you.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Programming Fundamentals Enumerations and Functions.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
More on Functions. Assignment 1 Let’s talk … -We always validate to ensure that our program accepts only valid input. - This is done as early in the program.
How does it work? What is it made of? What is it made of?
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
Functions.
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Lesson #6 Modular Programming and Functions.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions, Part 1 of 3 Topics Using Predefined Functions
Introduction to Classes and Objects
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
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 13

2 Previous lecture A menu driven program using – if else – switch case Nested case control structure – A calculator (add, subtract, multiply) – Factorial, prime, even/odd

3 Today’s lecture outline Introduction to structure programming Function definition Function call Function prototype

4 Structured programming It enables programmers to break complex systems into manageable components In C, these components are known as functions A function is a block of statements that perform a task Every C program can be thought of as a collection of these functions

5 Structured programming concepts Top-down design Code reusability Information hiding

6 Top-down design To demonstrate let see ATM (Automated Teller Machine) as an example Suppose your boss assigns you to program the software for a ATM system Question for you is where to begin as it's a large task filled with complexities and many details

7 ATM example - Cont. Top-down design helps you to design complex program The following steps demonstrate the top- down design process 1.Break the problem into small, manageable components, starting from the top. In C, the top component is the main() function from which other components are called

8 ATM example - Cont. 2.Identify all major components. For the ATM example, assume there are four major components: Display balance Deposit funds Transfer funds Withdraw funds 3. Decompose one major component at a time and make it more manageable and less complex

9 ATM example – Cont. 4. The withdraw funds component can be decomposed into following sub-components Get available balance Compare available balance to amount requested Update customer’s account Distribute approved funds Reject request Print receipt

10 ATM example – Cont. 5.Go even further with the decomposition process and divide the “distribute approved funds” component even smaller: Verify ATM funds exist Initiate mechanical processes Update bank records

11 Decomposing ATM system—top-down design

12 C programming main() { …. DisplayBalance() …. TransferFunds() …. } DisplayBalance() { ….. } TransferFunds() { ….. }

13 Cont. main() { …. printf(“Enter a No. ”); scanf(“%d”, &x); …. } printf( ….. ) { ….. } scanf( …. ) { ….. }

14 Code Reusability Code reusability is implemented as functions in C Consider the following list of components and subcomponents from the ATM example in the previous section – Get available balance – Compare available balance to amount requested – Update customer’s account – Distribute approved funds – Reject request – Print receipt

15 Cont. A possible number of transactions a customer might perform at a single visit to an ATM – Deposit money in account – Transfer funds from his account to his other account – Withdraw money from checking – Print balance – At least four occasions require to access the customer’s balance

16 Code reusability – other examples printf(….) function scanf(….) function system function – system(“pause”); – system(“cls”);

17 Information Hiding Information hiding is a conceptual process by which programmers hide implementation details into functions Functions can be seen as black boxes Black box is simply a component that performs a task You don't know how the black box performs the task data Formatted text to screen data Data assigned to variable

18 Function A function is a self-contained block of statements that perform a task Every C program can be thought of as a collection of these functions using a function is like hiring a person to do a specific job Sometimes the interaction with this person is very simple; sometimes it’s complex. – Suppose task is always performed exactly in the same way e.g. bimonthly servicing of your motorbike

19 Function call and definition main() { message(); printf(“Hello world \n”); } message() { printf(“Message function \n”); } Waiting Function call Function definition

20 Example program 1 Go to program

21 Points to remember C program must contains at least one function main() Execution of C program begins with main() function. main() If there are more than one function then one function must be main() There is no limit on number of functions in C program main() Functions in a program are called in sequence as mentioned in main() function main() After the execution of function control returns to main()

22 Example program 2 I am in main I am in italy I am in brazil I am in argentina I am back in italy I am finally back in main Press any key to continue

23 Function prototype Function prototypes tell C how your function will be built and used Function prototype contains following things about the function: – The data type returned by the function – The number of parameters received – The data types of the parameters – The order of the parameters

24 Cont. It is not always necessary – to send input as parameters to functions – to have functions return values In such case programmer mention that function are void of parameter and return value

25 Complete program Function prototype Function definition Function call

26 Important points C program is a collection of one or more functions A function gets called when the function name is followed by a semicolon. For example, A function is defined when function name is followed by a pair of braces in which one or more statements may be present

27 Cont. Any function can be called from any other function. Even main( ) can be called from other functions. For example,

28 Cont. A function can be called any number of times. For example

29 Cont. The order in which the functions are defined in a program and the order in which they get called need not necessarily be same. For example,

30 Cont. A function can call itself. Such a process is called ‘recursion’ A function cannot be defined in another function There are basically two type of functions – Library functions Ex. printf( ), scanf( ) etc. – User-defined functions Ex. argentina( ), brazil( ) etc.

31