CSC 1401 S1 Computer Programming I

43
1 CSC 1401 S1 Computer Programming I Hamid Harroud Hamid Harroud School of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn University University [email protected] http://www.aui.ma/~H.Harroud/CSC1401 Spring 2009

description

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

Transcript of CSC 1401 S1 Computer Programming I

Page 1: CSC 1401 S1 Computer Programming I

1

CSC 1401 S1 Computer Programming I

Hamid HarroudHamid HarroudSchool of Science and Engineering, Akhawayn School of Science and Engineering, Akhawayn

[email protected]

http://www.aui.ma/~H.Harroud/CSC1401

Spring 2009

Page 2: CSC 1401 S1 Computer Programming I

Lecture 1: Introduction

Top-Down Design with Functions: Modular Programming

Lecture 6

Page 3: CSC 1401 S1 Computer Programming I

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

Page 4: CSC 1401 S1 Computer Programming I

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>

Page 5: CSC 1401 S1 Computer Programming I

Square Root computation

Page 6: CSC 1401 S1 Computer Programming I

Square Root Program

Page 7: CSC 1401 S1 Computer Programming I

Square Root Program (cont’d)

Page 8: CSC 1401 S1 Computer Programming I

Some Mathematical Library Functions

Page 9: CSC 1401 S1 Computer Programming I

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.

Page 10: CSC 1401 S1 Computer Programming I

Example: Draw Simple Diagrams

House and Female Stick Figure

Page 11: CSC 1401 S1 Computer Programming I

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

Page 12: CSC 1401 S1 Computer Programming I

Structure Chart

Page 13: CSC 1401 S1 Computer Programming I

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.

Page 14: CSC 1401 S1 Computer Programming I

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.

Page 15: CSC 1401 S1 Computer Programming I

Example: Function Prototypes & Main function for Stick Figure

Page 16: CSC 1401 S1 Computer Programming I

Function Definitions

Page 17: CSC 1401 S1 Computer Programming I

Order of Execution of Function Subprograms and Main function

1. When the computer executes a function call statement, it transfers the control to the function.

2. The computer allocates memory for variables used in the function and executes the statements in the function body.

3. After the execution of the function, the control is returned to the calling function and the allocated memory is released.

Page 18: CSC 1401 S1 Computer Programming I

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.

Page 19: CSC 1401 S1 Computer Programming I

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.

Page 20: CSC 1401 S1 Computer Programming I

Void Functions with Input Arguments We can create functions that receive input

arguments but do not return any results.

Page 21: CSC 1401 S1 Computer Programming I

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.

Page 22: CSC 1401 S1 Computer Programming I

Functions with Input Arguments and a Single Result.

Page 23: CSC 1401 S1 Computer Programming I

Example: Single Input & Single Output

Page 24: CSC 1401 S1 Computer Programming I

Effect of Executing circum = find_circum (radius);

Page 25: CSC 1401 S1 Computer Programming I

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.

Page 26: CSC 1401 S1 Computer Programming I

The Function Data Area

Page 27: CSC 1401 S1 Computer Programming I

The Function Data Area

Page 28: CSC 1401 S1 Computer Programming I

Example Using functions, write a program that computes

the area of the gray surface.

r1

r2

Page 29: CSC 1401 S1 Computer Programming I

Variable Scope

Page 30: CSC 1401 S1 Computer Programming I

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

Page 31: CSC 1401 S1 Computer Programming I

Local/Global Variables

Page 32: CSC 1401 S1 Computer Programming I

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 )

Page 33: CSC 1401 S1 Computer Programming I

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

Page 34: CSC 1401 S1 Computer Programming I

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

Page 35: CSC 1401 S1 Computer Programming I

What would the output?

Page 36: CSC 1401 S1 Computer Programming I

Functions with ‘Output Parameters’

What about this function?

void f(int *p) {int a = *p, *q = p;

*p = 7;

*p = a**q;

}

Page 37: CSC 1401 S1 Computer Programming I

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

Page 38: CSC 1401 S1 Computer Programming I

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

Page 39: CSC 1401 S1 Computer Programming I

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

Page 40: CSC 1401 S1 Computer Programming I

The ‘Swap’ function

Page 41: CSC 1401 S1 Computer Programming I

Self-Check1

Page 42: CSC 1401 S1 Computer Programming I

Self-Check 2

Page 43: CSC 1401 S1 Computer Programming I

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