Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.

Slides:



Advertisements
Similar presentations
Chapter 6: User-Defined Functions I
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
Chapter 6: User-Defined Functions I
Functions:Passing Parameters by Value Programming.
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
Functions. COMP104 Lecture 13 / Slide 2 Function Prototype * The function prototype declares the interface, or input and output parameters of the function,
1 Lecture 17:User-Definded function II Introduction to Computer Science Spring 2006.
Functions Modules in C++ are called functions and classes
Chapter 6: Functions.
Programming in C++ Lecture Notes 6 Void Functions (Procedures) Andreas Savva.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
Chapter 6: User-Defined Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
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.
Section 4 - Functions. All of the programs that we have studied so far have consisted of a single function, main(). However, having more than one function.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
CHAPTER 6 USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
4.3 Functions. Functions Last class we talked about the idea and organization of a function. Today we talk about how to program them.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
User-Defined Functions (cont’d) - Reference Parameters.
USER-DEFINED FUNCTIONS I. In this chapter, you will: Learn about standard (predefined) functions and discover how to use them in a program Learn about.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
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.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
CHAPTER 6 USER-DEFINED FUNCTIONS I
Chapter 6: User-Defined Functions I
Chapter 6: User-Defined Functions I
User-Defined Functions
User Defined Functions
Chapter 5 Function Basics
Value returning Functions
Review for Final Exam.
CS150 Introduction to Computer Science 1
Chapter 6: User-Defined Functions I
Review for Final Exam.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

Modular Programming – User Defined Functions

CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement

CSCE 1063 General Functions’ Guidelines To use any function in general, you need to:  include the correct header file  know the name of the function  know the number of parameters/arguments, if any  know the data type of each parameter  know the data type of the value computed by the function, called the type of the function A value-returning function is either used in an assignment statement or in an output statement.

CSCE 1064 User-Defined Functions You can define two types of functions: 1.“void” function that does not return a value (and does not have a data type). 2.Value-returning function that has a data type. A function is defined by writing its heading and body. void drawCircle() { cout << “ * “ << endl; cout << “* *“ << endl; } Function name Function type Function statements

CSCE 1065 User-Defined Functions (cont’d)  You need to declare function prototypes (functions’ headings without the body of the functions) before your main() function.  Like standard functions, you need to call the function from your main() to activate it.  The function definition should come after your main().

CSCE 1066 #include using namespace std; void drawCircle(); void main() { cout << “ “ << endl; drawCircle(); cout << “ “ << endl; } void drawCircle() { cout << “ * “ << endl; cout << “* *“ << endl; } Function prototype Function heading Function call Execution

CSCE 1067 Functions With Arguments #include using namespace std; void drawCircleChar(char symbol); void main() { cout << “ “ << endl; drawCircleChar(‘*’); cout << “ “ << endl; drawCircleChar(‘o’); } void drawCircleChar(char symbol) { cout << “ “ << symbol << endl; cout << symbol << “ “ << symbol << endl; cout << “ “ << symbol << “ “ << symbol << endl; } Formal parameter Parameter Actual parameter

CSCE 1068 Value-Returning Functions The syntax for defining a value-returning function is: functionType functionName(formal parameter list) { statements }  functionType – data type of the value returned by the function  formal parameter - a variable declared in the function heading  actual parameter - a variable or expression listed in a call to a function

CSCE 1069 Value-Returning Functions (cont’d)  The syntax of the formal parameter list is: dataType identifier, dataType identifier,...  The syntax for a function call is: functionName(actual parameter list)  The syntax for the actual parameter list is: expression or variable,expression or variable,...  There is a one-to-one correspondence between actual and formal parameters  The formal parameter list can be empty.

CSCE The return Statement  Once the function computes the value, the function returns this value via the return statement  The syntax of the return statement is: return expression or variable;  When a return statement executes in a function, the function immediately terminates and the control goes back to the caller  When a return statement executes in the function main(), the program terminates

CSCE Exercise Write a complete C++ program, for an algorithm that calculates the average of two numbers by the use of a value returning function (computeAverage). The main function should input the two numbers, as well as outputting the average.

CSCE #include using namespace std; float computeAverage(float num1, float num2); int main() { float x, y, av; cout << “Please enter two numbers:“ << endl; cin >> x >> y; av = computeAverage(x,y); cout << “The average of the two numbers is:“ << av << endl; return 0; } float computeAverage(float num1, float num2) // 2 parameters { // Compute the average of the data. return ((num1 + num2) / 2.0); } // end computeAverage function Actual parameters Formal parameters

CSCE Argument Correspondence Actual Argument x y Corresponds to Formal Argument num1 num2

CSCE Tracing Exercise #include using namespace std; int Blend( int red, int green ); // prototype void main() { int red = 5, blue; blue = Blend(3, red + 1); cout << red << ' ' << blue << '\n'; blue = Blend(blue, red); cout << red << ' ' << blue << '\n'; } int Blend( int red, int green ) { int yellow; cout << “enter Blend “ << red <<‘ ‘ << green << ‘\n’; yellow = red + green; cout << “leave Blend “ << red <<‘ ‘ << green << ‘\n’; return (yellow + 1); } Output: enter Blend 3 6 leave Blend enter Blend 10 5 leave Blend

CSCE Next lecture we will continue the Modular Construct in C++