1 / 43

CSC 1401 S1 Computer Programming I

CSC 1401 S1 Computer Programming I. Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009. Top-Down Design with Functions: Modular Programming. Lecture 6. Lecture 1: Introduction. Objectives.

asta
Download Presentation

CSC 1401 S1 Computer Programming I

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 1401 S1Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University h.harroud@aui.ma http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009

  2. Top-Down Design with Functions: Modular Programming Lecture 6 Lecture 1: Introduction

  3. Objectives • Building Programs from Existing functions • Some Mathematical Library Functions • Create you own functions • Top-Down Design & Structure Charts • Declare Functions • Order of Execution of Function Subprograms and Main function • The Function Data Area

  4. Predefined Functions and Code Reuse • A primary goal of software engineering is to write error-free code. • Code reuse: reusing program fragments that have been written and tested. • C library functions provide most commonly used functions. • e.g., mathematical library <math.h> • To use existing C library functions, we have to include the header file of the corresponding library. • e.g., #include <math.h>

  5. Square Root computation

  6. Square Root Program

  7. Square Root Program (cont’d)

  8. Some Mathematical Library Functions

  9. Top-Down Design & Structure Charts • Most likely a problem is complex, and we have to break up the problem into subproblems. • Top-Down Design is a method in which we break a problem into subproblems, and derive the solution for the original problem. • Structure Chart is a documentation tool that shows the relationships among the subproblems of a problem.

  10. Example: Draw Simple Diagrams • House and Female Stick Figure

  11. House and Female Stick Figure • The house consists of a triangle without its base and a rectangle. • The female stick figure consists of a circle, a triangle, and a triangle without its base. • Suppose we are given four basic drawing components (functions). • A circle • Parallel lines • A base line • Intersecting lines

  12. Structure Chart

  13. Use and define your own functions • One way to implement top-down design is by defining your own functions. • Usually a subproblem is solved by a corresponding subprogram. • Functions without Arguments • Simple Functions that have no arguments and return no value.

  14. Declare Functions without Arguments • Declaration: ftype fname(void); • The identifier ftype specifies the data type of the function results. • Example: void fname(void); • After fname() is called, the execution of the program jumps to the subprogram defined by fname.

  15. Example: Function Prototypes & Main function for Stick Figure

  16. Function Definitions

  17. Order of Execution of Function Subprograms and Main function • When the computer executes a function call statement, it transfers the control to the function. • The computer allocates memory for variables used in the function and executes the statements in the function body. • After the execution of the function, the control is returned to the calling function and the allocated memory is released.

  18. Advantages of Using Function Subprograms • Procedural Abstraction • The main function consists of a sequence of function calls. • We defer implementation details until we are ready to write the subprogram. • We can focus on one function at a time. • Reuse of Function Subprograms • The subprogram can be executed more than once in a program • Decrease the length of code and chance of error.

  19. Function with Arguments • The arguments of a function are used to transfer information between the calling and called functions. • Input Arguments are used to pass information into a subprogram from the calling function. • Output Arguments are used to return results to the calling function.

  20. Void Functions with Input Arguments • We can create functions that receive input arguments but do not return any results.

  21. Example • Write a C program that uses a function called display_circle_info that displays the diameter, the circumference and the area of a circle with radius R entered by the user.

  22. Functions with Input Arguments and a Single Result.

  23. Example: Single Input & Single Output

  24. Effect of Executing circum = find_circum (radius);

  25. Example: Multiple Input Arguments & Single Output • Function scale multiples the first input argument by 10 raised to the power indicated by the second input argument.

  26. The Function Data Area

  27. The Function Data Area

  28. Example • Using functions, write a program that computes the area of the gray surface. r1 r2

  29. Variable Scope

  30. Local/Global Variables •  Reminder: What happens when a function is called?: • A separate ‘memory-Space’ is created for the called function! • The var ‘a’ is declared inside the ‘main’ • Hence, since the spaces for the ‘functions’ are separated, a will not be known in the ‘increment’ space • ‘a’ has a Local Scope. The one of the ‘main’ function is visible only to main • ‘a’ is a ‘Local Variable’  Local Variables die when the embedding function ends • How to make a variable globally visible? • Global Variables • Declared outside all functions: Before the main, in the definitions part

  31. Local/Global Variables

  32. How to make a local variable visible? Pointers! • Since a local variable is not visible to other functions, its address should be used ! • Pointers: Variables that stores addresses • Addresses of What? • Of other variables. • Hence, ALL what a pointer is, is a ‘VARIABLE’ • Like any other variable, pointers differ in the data type to be stored. i.e, which kind of address? • Addresses of char variables ( char *ptr ) • Addresses of integer ( int *ptr ) • Addresses of double ( double *ptr )

  33. Understanding POINTERSThe ‘Dereference’ operator ‘*’ • Always to remember: • A pointer is a variable that stores addresses • Consider the following program fragment: • int a, *ptr; • Can make the following assignments? • ptr = a; ptr = a; • What is the valid assignments, since pointers store addresses? • ptr = &a; • I can also make a = *ptr?!!! • ‘*’ is used to refer to the value stored into a variable pointed to by ptr. i.e. whose address is stored in ptr

  34. The Dereference Operator • int a, b, *ptr; • a = 2; b = 7; ptr = &b; • p = *ptr; • printf(‘ %d’, a); • What about scanf(‘%d’, ptr)? • b will have the scanned value • What if I proceed as follows? • ptr = &a; • scanf(‘%d’, ptr);

  35. What would the output?

  36. Functions with ‘Output Parameters’ • What about this function? void f(int *p) { int a = *p, *q = p; *p = 7; *p = a**q; }

  37. « Passing by Value » • Consider the following program: • Why? • a is a ‘Local Variable’! • Only its value – a copy of it!! - is passed to the ‘increment’ function • This is why it is not affected • a is said to be ‘passed by value’ • Solution:  pass by Reference (by address),

  38. Passing by Reference • The ‘increment’ function is said to have an ‘Output Parameter’! • Why ‘Output’? • Because, ‘a’ changed’ its value. • The function outputs the new value • In fact, a is said to be an ‘Input/Output’ parameter

  39. Passing by Value vs. By Reference • Since the ‘return’ can ‘output’ only one value, passing by reference is the means for ‘outputting’ more than one value. • Rule of thumb: • If you do not want your input variable to be affected, you need just the value/copy:  Pass it by value • If you want your input variable to be affected, (Hence, becoming an Input/Output variable),  Pass it by reference. i.e, pass its address • Example: • Write a function that swaps the contents of 2 variables

  40. The ‘Swap’ function

  41. Self-Check1

  42. Self-Check 2

  43. Summary • Building Programs from Existing functions • Some Mathematical Library Functions • Create you own functions • Top-Down Design & Structure Charts • Declare Functions • Order of Execution of Function Subprograms and Main function • The Function Data Area

More Related