Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University."— Presentation transcript:

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

2  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

3  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

4  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

5 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

6 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 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

8  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

9  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

10 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

11  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

12 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

13  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

14  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

15 #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

16  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

17  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 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; }

19  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

20  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 21 #include int addTwoNumbers(int, int); int main(void) { printf(“%d”, addTwoNumbers(4,5)); } int addTwoNumbers(int operand1, int operand2) { return operand1 + operand2; }

22  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

23  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

24  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

25  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

26  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

27  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

28  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

29  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

30  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

31  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

32  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

33  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

34  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


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

Similar presentations


Ads by Google