CS149D Elements of Computer Science

Slides:



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

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Copyright © 2002 W. A. Tucker1 Chapter 3 Lecture Notes Bill Tucker Austin Community College COSC 1315.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 6: User-Defined Functions I
CS 117 Spring 2002 Functions Hanly - Chapter 5 Friedman-Koffman - Sections & Chapter 6.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 06 (Part I) Functions and an Introduction to Recursion.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Functions Outline 5.1Introduction 5.2Program Modules.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
KIC/Computer Programming & Problem Solving 1.  Introduction  Program Modules in C  Math Library Functions  Functions  Function Definitions  Function.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
1 ICS103 Programming in C Lecture 8: Functions I.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 UMBC CMSC 104, Section Fall 2002 Functions, Part 1 of 3 Topics Top-down Design The Function Concept Using Predefined Functions Programmer-Defined.
Programming Fundamentals Enumerations and Functions.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Dale Roberts CSCI N305 Functions Declarations Department of Computer and Information Science, School of Science, IUPUI.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
FUNCTIONS IN C++.
Programming Fundamentals Lecture #7 Functions
CSC113: Computer Programming (Theory = 03, Lab = 01)
Dr. Shady Yehia Elmashad
CS149D Elements of Computer Science
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Dr. Shady Yehia Elmashad
Functions Declarations CSCI 230
CS149D Elements of Computer Science
One-Dimensional Array Introduction Lesson xx
Chapter 6: User-Defined Functions I
Functions Divide and Conquer
CS150 Introduction to Computer Science 1
CS148 Introduction to Programming II
CS149D Elements of Computer Science
Functions Imran Rashid CTO at ManiWeber Technologies.
CS149D Elements of Computer Science
CS148 Introduction to Programming II
CPS125.
Presentation transcript:

CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture 23: 11/21/2002 Lecture 23: 11/21/2002 CS149D Fall 2002

Outline Modular Programming with Functions (Chapter 4, Section 5.2) Lecture 23: 11/21/2002 CS149D Fall 2002

Library Functions Examples of library functions we saw before sqrt  calculates the square root sqrt is a value-returning function which takes one parameter x = sqrt(y) We did not have to implement the sqrt function ourselves, but had to include “math.h” for its definition We could reuse over and over again the sqrt function, while we are sure we will get the correct result Lecture 23: 11/21/2002 CS149D Fall 2002

Programmer-defined Functions For longer complex problems we write programs that use the main function and additional functions instead of using one long main function What are the advantages? A function can be written and tested separately from the rest of the program (we can divide writing large programming projects between a group of programmers) An individual solution is smaller than the complete program (remember the Divide and Conquer approach) Once a function has been tested, it can be reused in new programs without being retested (reusability) We can think of our functions as black boxes that have specific input and that compute specific information and maybe return a value (abstraction) Lecture 23: 11/21/2002 CS149D Fall 2002

Function definition return_type function_name (parameters declaration) //Note that there is no ; here { //Function body //if return_type is the word void, this function is a void function, it does not return a value } void printHello () { cout << “*************************”; cout << “ Hello World”; } return; // this function computes x to the nth power. // n >= 0 and result <= maximum integer number int power (int x, int n) { int result = 1; while (n > 0) result = result *x; n--; } return result; Lecture 23: 11/21/2002 CS149D Fall 2002

Function Call from within Program1/2 void main () { int x = 10, n = 3,y; int power (int x,int n); //Function prototype, note the ; at the end of the statement y = power (x,n); //Function call x and y are called arguments (actual parameters) cout << y; //can just write cout << power(x,n); if value need not be stored in y } // this function computes x to the nth power. int power (int x, int n) // x and n are called parameters (formal parameters) int result = 1; // result is a local variable to the function power while (n > 0) result = result *x; n--; return result; Lecture 23: 11/21/2002 CS149D Fall 2002

Function Call from within Program2/2 void main () { …… …….. y = power (x,n); //Function call cout << y; ……… } // this function computes x to the nth power. int power (int x, int n) ……. return result; Function executes Lecture 23: 11/21/2002 CS149D Fall 2002

Call by Value // this function computes x to the nth power. int power (int x, int n) { int result = 1; // result is a local variable to the function power while (n > 0) result = result *x; n--; } return result; Question: Can a function change the value of its arguments? This type of function call is a call by value, which means the function can not change the value of its arguments, even if it attempted to. The change in the value of n in the function is not visible to the caller function (main) An exception is when the parameter is an array Lecture 23: 11/21/2002 CS149D Fall 2002

Arrays as Function Parameters1/2 //Function prototype. Note you do not need to specify the size of the array double average_Array (double A[], int number); //Function prototype //main void main () { const int N = 100; double array[N],average; …….. average = averageArray(100,N) cout << average; } // this function computes the average of an array of doubles double average_Array (double A[], int number) double sum; for (int k = 0; k < number ; k++) sum = sum + A[k]; return sum/number; Lecture 23: 11/21/2002 CS149D Fall 2002

Arrays as Function Parameters2/2 When a simple variable is used as a parameter, the value is passed to the formal parameter in the function, and thus the original value can not be changed. When an array is passed as a parameter to a function, the memory address of the array (the address of the first element in the array) is passed to the function, instead of the entire set of values The function references values in the original array (call by reference), which means the function can change the values of the elements if desired void FillArray (int A[], int N); //Function prototype void main () { int my_array[100]; FillArray(my_array,100); //Print array elements } void FillArray (int A[], int N) for (int k = 0; k < N ; k++) A[k] = k; Lecture 23: 11/21/2002 CS149D Fall 2002