User Defined Functions

Slides:



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

Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
User Defined Functions
Chapter Five Functions
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Chapter 6: User-Defined Functions I
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 Modules in C++ are called functions and classes
CMSC 104, Version 8/061L18Functions1.ppt Functions, Part 1 of 4 Topics Using Predefined Functions Programmer-Defined Functions Using Input Parameters Function.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Chapter 6: User-Defined Functions
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Manesh T 2 Chapter Topics Define Function Standard (Predefined) Functions User-Defined Functions Parts of functions.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Functions Chapter 7 2 Hope you can function! What is R2D2 doing here? What is his function? Who is Nibble? Can he function? IS he a function? Who is.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
CPS120: Introduction to Computer Science Functions.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
Chapter 3 Top-Down Design with Functions Part II J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National.
Manish K Parmar PGT (CS) K V VVNagar Thursday, December 24, 2015 Lesson on USER DEFINED FUNCTION IN C++ Presented by Manish K Parmar PGT Computer Science.
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.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
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.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions, Part 1 of 3 Topics  Using Predefined Functions  Programmer-Defined Functions  Using Input Parameters  Function Header Comments Reading 
Chapter 6: User-Defined Functions I
Function Topic 4.
Predefined Functions Revisited
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
FUNCTIONS IN C++.
Functions in C Mrs. Chitra M. Gaikwad.
CSCI 161: Introduction to Programming Function
User-Defined Functions
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
User Defined Functions
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
Functions, Part 1 of 3 Topics Using Predefined Functions
FUNCTION CSC128.
Chapter 6: User-Defined Functions I
Functions, Part 1 of 3 Topics Using Predefined Functions
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
Functions I Creating a programming with small logical units of code.
CPS125.
Presentation transcript:

User Defined Functions Chapter 6

Chapter Topics Standard (Predefined) Functions User-Defined Functions Value-Returning Functions The return Statement Function Prototype Flow of Execution

Top-Down Structured Design with Functions Recall two types of functions Value returning function computes a single value returns value to calling code uses return command Void function (procedure) called as a statement executes some task This chapter focuses on the value returning function

Advantages of Using Functions To help make the program more understandable To modularize the tasks of the program building blocks of the program Write a module once those lines of source code are called multiple times in the program

Advantages of Using Functions While working on one function, you can focus on just that part of the program construct it, debug it, perfect it. 5. Different people can work on different functions simultaneously. 6. If a function is needed in more than one place in a program, or in different programs, you can write it once and use it many times

Standard (Predefined) Functions Part of the C++ language Provided in function libraries Examples: abs(x), sin(x), log(x), pow( x, n) These functions will return a value To be printed cout << sin (x); To be assigned y = pow (3, 4.5); To be used in an expression 3.14 * sqr(r) Make sure to use the required #include file

Predefined Functions View sample program

Value-Returning Functions All the properties together make up the definition of the function Information provided by the heading of the function For the compiler to use a function you have written, it must know when it finds that function call in your source code … 1. The name of the function 2. The number of parameters, if any 3. The data type of each parameter 4. Data type of the value returned by the function 5. The code required to do the calculation Information provided in the body of the function

Value-Returning Functions Consider a function for the area of a circle: double circleArea (double radius) { return 3.14159 * radius * radius; } Note the Heading (type, name, parameters) The body The return statement

Parameters Function definition syntax: functionType functionName (formal parameter list) { statements } Call (invocation of the function) cout << "Enter radius for circle area -> "; cin >> radius; area = circleArea (radius); Parameters in the declaration : formal parameters Parameters in the call: actual parameters

The return Statement A value returning statement must have a return statement Else a warning from the compiler Also the function will actually return a "garbage" value Syntax: return expression; View example

Function Prototype Recall that the compiler must know certain things about your function When it finds a function call in your source code Must know information in heading Your program must have at least the heading of a function before it is invoked Usually listed before function main ( ) View example

Alternative to Prototype Also possible to place whole function definition before main () This is a requirement in some languages (Pascal) Either is acceptable in this class There may be a standard required of programmers within a particular organization View Example

Flow of Control First statement executed in any program is the first statement in the function main( ) When another function called logical control passed to first statement in that function’s body program proceeds through sequence of statements within the function When last statement of function executed control returns to where function was called control given to next command after the call

Flow of Control void main ( ) { . . . print_summary (rpt_total); revenue = rpt_total * .72675; . . . } void print_summary (int total) { . . . cout << . . . } - first statement of main - function call, jumps to first statement of that function - proceeds through function - returns to next statement after call