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.

## CSC 1401 S1 Computer Programming I

An Image/Link below is provided (as is) to download presentation Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. 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. Function Definitions

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

17. 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.

18. 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.

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

20. 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.

21. Functions with Input Arguments and a Single Result.

22. Example: Single Input & Single Output

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

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

25. The Function Data Area

26. The Function Data Area

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

28. Variable Scope

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

30. Local/Global Variables

31. 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 )

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

33. 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);

34. What would the output?

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

36. « 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),

37. 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

38. 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

39. The ‘Swap’ function

40. Self-Check1

41. Self-Check 2

42. 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