440 likes | 651 Views
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.
E N D
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
Top-Down Design with Functions: Modular Programming Lecture 6 Lecture 1: Introduction
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
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>
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.
Example: Draw Simple Diagrams • House and Female Stick Figure
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
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.
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.
Example: Function Prototypes & Main function for Stick Figure
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.
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.
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.
Void Functions with Input Arguments • We can create functions that receive input arguments but do not return any results.
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.
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.
Example • Using functions, write a program that computes the area of the gray surface. r1 r2
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
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 )
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
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);
Functions with ‘Output Parameters’ • What about this function? void f(int *p) { int a = *p, *q = p; *p = 7; *p = a**q; }
« 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),
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
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
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