305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University.

Slides:



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

Introduction to C Programming
Spring Semester 2013 Lecture 5
Modular Programming With Functions
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
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.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
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.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
CS 201 Functions Debzani Deb.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Chapter 6: User-Defined Functions I
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
Introduction to Methods
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
1 CSC103: Introduction to Computer and Programming Lecture No 13.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
chap13 Chapter 13 Programming in the Large.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
1 CSC103: Introduction to Computer and Programming Lecture No 14.
1 ICS103 Programming in C Lecture 7: Introduction to Functions.
CPS120: Introduction to Computer Science Functions.
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.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Introduction to Classes, Objects, Methods and strings
© 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.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
CCSA 221 Programming in C CHAPTER 8 – PART 1 WORKING WITH FUNCTIONS 1.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
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.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Introduction to C Programming Language
Chapter 5 - Functions Outline 5.1 Introduction
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Functions Inputs Output
Functions Declarations CSCI 230
Chapter 4 void Functions
Chapter 6 - Functions Outline 5.1 Introduction
6 Chapter Functions.
Introduction to Computing Lecture 08: Functions (Part I)
CPS125.
Presentation transcript:

Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University

 Structured programming enables programmers to break complex systems into manageable components.  In C, these components are known as functions.  The most relevant structured programming concepts are: – Top-down design – Code reusability – Information hiding 2

 In C, top-down design enables analysts and programmers to define detailed statements about a system’s specific tasks.  Top-down design experts argue that humans are limited in their multitasking capabilities. Those who excel at multi-tasking and enjoy the chaos it brings are generally not programmers.  Programmers are inclined to work on a single problem with tedious detail. 3

 To demonstrate top-down design, an ATM design is used as an example. 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. 2.Identify all major components. For the ATM example, assume there are 4 major components: Display balance Deposit funds Transfer funds Withdraw funds 4

3.Decompose one major component at a time and make it more manageable and less complex. 4.The withdraw funds component can be broken down into smaller pieces, such as these: Get available balance Compare available balance to amount requested Update customer’s account Distribute approved funds Reject request Print receipt 5

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 6

7 ATM Deposit Funds Display Balance Transfer Funds Withdraw Funds Get Available Balance Compare Balance to Request Update Account Disperse Funds Reject Request Print Receipt Verify ATM Funds Exist Initiate Mechanical Process Update Bank Records

 In the world of C programming, code reusability is implemented as functions.  Specifically, programmers create user- defined functions for problems that generally need frequently used solutions.  Example: “Get Available Balance” in ATM  Example: printf() sqrt() strcpy() 8

 Information hiding is a conceptual process by which programmers conceal implementation details into functions.  Functions can be seen as black boxes.  A black box is simply a component, logical or physical, that performs a task. You don't know how the black box performs the task; you just simply know how to use it. 9

string list of parameters 10 sqrt() strcmp() printf() Input floating point data Input string Output square root value Output integer Input Output print to screen

 Function prototypes tell C how your function will be built and used.  It is a common programming practice to construct your function prototype before the actual function is built.  Programmers must think about the desired purpose of the function, how it will receive input, and how and what it will return. 11

int findMax(int, int);  Examples float addTwoNumbers(int, int); double myFunction(char, int, int); void printBalance(int); int createRandomNumber(void); 12 return typefunction namelist of parameters

 Function prototypes should be placed outside the main() function and before the main() function starts. #include int addTwoNumbers(int, int); int subtractTwoNumbers(int, int); int main(void) { } 13

 Function definitions implement the function prototype.  The first line of the function definition (also known as the header) resembles the function prototype, with minor exceptions. int addTwoNumbers(int operand1, int operand2) { int result = operand1 + operand2; return result; } 14 return type function nameparameters declarations return value

#include int addTwoNumbers(int, int); int main(void) { printf("Nothing happening in here."); } int addTwoNumbers(int operand1, int operand2) { return operand1 + operand2; } 15 Function prototype Function definition

 C functions generally adhere to the following rules: 1. Every function must have a name. 2. Function names are made up and assigned by the programmer (you!) following the same rules that apply to naming variables. 3. All function names have one set of parentheses immediately following them. This helps you (and C) differentiate them from variables. The parentheses might or might not contain something. 4. The body of each function, starting immediately after the closing parenthesis of the function name, must be enclosed by braces. This means that a block containing one or more statements makes up the body of each function. 16

 It’s now time to put your functions to work with function calls.  You work with your user-defined functions the same way you work with other C library functions such as printf() and scanf(). 17

18 #include int addTwoNumbers(int, int); int main(void) { int iResult; iResult = addTwoNumbers(4,5); printf(“%d”, iResult); } int addTwoNumbers(int operand1, int operand2) { return operand1 + operand2; }

 Exercise 1: Function call 1.Start and prepare to run a new project in VC++ Express 2.Copy the code from addTwoNumbers.c and paste into the code editor. 3.Run the code and observe the result. 4.Let’s play with the code. 5.Modify the code to 1.Subtract, multiply, or divide numbers 2.Receive more arguments 19

 Function calls can also be placed in other functions. To demonstrate, study the next block of code that uses the same addTwoNumbers() function call inside a printf() function. 20

21 #include int addTwoNumbers(int, int); int main(void) { printf(“%d”, addTwoNumbers(4,5)); } int addTwoNumbers(int operand1, int operand2) { return operand1 + operand2; }

 Exercise 2: Function calls 2 1.Start and prepare to run a new project in VC++ Express 2.Copy the code from functions.c and paste into the code editor. 3.Run the code and observe the result. 4.Let’s play with the code. 22

 Variable scope identifies and determines the life span of any variable in any programming language.  When a variable loses its scope, it means its data value is lost.  The two common types of variables scopes in C are local and global. 23

 Local variables are defined in functions, such as the main() function, and lose their scope each time the function is executed. #include void main(void) { int num = 5; printf(“%d”, num); } 24

 Because local scope variables are tied to their originating functions, you can reuse variable names in other functions without running the risk of overwriting data. 25

 Exercise 3: Local variables 1.Start and prepare to run a new project in VC++ Express 2.Copy the code from local.c and paste into the code editor. 3.Run the code and observe the result. 4.Let’s play with the code. 26

 Locally scoped variables can be reused in other functions without harming one another’s contents.  At times, however, you might want to share data between and across functions.  To support the concept of sharing data, you can create and use global variables.  Global variables are created and defined outside any function, including the main() function. 27

 Exercise 4: Global variables 1.Start and prepare to run a new project in VC++ Express 2.Copy the code from global.c and paste into the code editor. 3.Run the code and observe the result. 4.Let’s play with the code. 28

 It is not wise, however, to use global variables liberally as they can be error prone and deviate from protecting data.  Using global variables allows all functions in a program file to have access to the same data, which goes against the concept of information hiding. 29

 Be careful about creating local variables with the same name in the same function.  If you define a local variable early in a function and then define another local variable with the same name inside a new block, C uses only the innermost variable, until its block ends. 30

 Exercise 5: Nested local variables 1.Start and prepare to run a new project in VC++ Express 2.Copy the code from nested_local.c and paste into the code editor. 3.Run the code and observe the result. 4.Let’s play with the code. 31

 The terms automatic and static describe what happens to local variables when a function returns to the calling procedure.  By default, all local variables are automatic, meaning that they are erased when their function ends.  To declare a variable as an automatic variable, prefix its definition with the term auto. The auto keyword is optional with local variables because they are automatic by default. 32

 Local static variables do not lose their values when their function ends. They remain local to that function.  When the function is called after the first time, the static variable's value is still in place.  You declare a static variable by placing the static keyword before the variable's definition. 33

 Exercise 6: Static variables 1.Start and prepare to run a new project in VC++ Express 2.Copy the code from static.c and paste into the code editor. 3.Run the code and observe the result. 4.Let’s play with the code. 34