1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

Introduction to C Programming
Spring Semester 2013 Lecture 5
Modular Programming With Functions
Programming and Data Structure
Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Chapter 3 Top-Down Design with Functions Part I J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Chapter 6 Modular Programming J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS 201 Functions Debzani Deb.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 3 Top-Down Design with Functions Dr. J.-Y. Pan Dept. Comm. Eng. Nat. Chung Cheng Univ.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
 2007 Pearson Education, Inc. All rights reserved C Functions.
CS 201 Functions Debzani Deb.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Topic 4 – Programmer- Defined Functions. CISC 105 – Topic 4 Functions So far, we have only seen programs with one function, main. These programs begin.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Lecture 5 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Procedural programming in Java Methods, parameters and return values.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
chap3 Chapter 3 Top-Down Design with Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 5 Modular Design and Function C Programming for Scientists & Engineers with Applications by Reddy & Ziegler.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
1 CSC103: Introduction to Computer and Programming Lecture No 17.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
CS1001 Programing Fundamental Lecture 5 Top-Down Design with Functions
User-Written Functions
Function Topic 4.
Chapter 6 Modular Programming Dr. J.-Y. Pan Dept. Comm. Eng.
Deitel- C:How to Program (5ed)
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Lec8.
CS149D Elements of Computer Science
ENERGY 211 / CME 211 Lecture 8 October 8, 2008.
Chapter 6 Modular Programming chap6.
Top-Down Design with Functions
Top-Down Design with Functions
Presentation transcript:

1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University Spring 2009

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

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 To use existing C library functions, we have to include the header file of the corresponding library. e.g., #include

Square Root computation

Square Root Program

Square Root Program (cont ’ d)

Some Mathematical Library Functions

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

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

Structure Chart

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

Function Definitions

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.

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.

Functions with Input Arguments and a Single Result.

Example: Single Input & Single Output

Effect of Executing circum = find_circum (radius);

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.

The Function Data Area

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

Variable Scope

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

Local/Global Variables

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 POINTERS The ‘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);

What would the output?

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

The ‘Swap’ function

Self-Check1

Self-Check 2

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