Single-Result Functions Section 5.1 02/25/11. Programming Assignment On website.

Slides:



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

Computer Science 1620 Function Overloading. Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler.
1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
Chapter 5 Functions.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Writing and Testing Programs Drivers and Stubs Supplement to text.
 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 
CS 201 Functions Debzani Deb.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Agenda Review C++ Library Functions Review User Input Making your own functions Exam #1 Next Week Reading: Chapter 3.
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 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
L function n predefined, programmer-defined l arguments, (formal) parameters l return value l function call, function invocation l function definition.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
FUNCTIONS IN C++. DEFINITION OF A FUNCTION A function is a group of statements that together perform a task. Every C++ program has at least one function,
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.
Unit 3 Lesson 11 Passing Data and Using Library Functions Textbook Authors: Knowlton, Barksdale, Turner, & Collings PowerPoint Lecture by Dave Clausen.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
1 MODULAR DESIGN AND ABSTRACTION. 2 SPECIFYING THE DETAILS OF A PROBLEM INTO A RELATED SET OF SMALLER PROBLEMS.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
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 - What Is A Function? - Advantages Function Declaration
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
General Computer Science for Engineers CISC 106 Lecture 12 James Atlas Computer and Information Sciences 08/03/2009.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
CSC1201: Programming Language 2 1 Functions. 2 Function declaration: return_type FuncName( Type arg1, Type arg2,….. Type argN) { function body } A program.
1 Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function Definition float sqrt(float x) { // compute.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Predefined Functions Revisited
FIGURE 4-10 Function Return Statements
Chapter 5 Function Basics
CMPT 201 Functions.
User-defined Functions
FIGURE 4-10 Function Return Statements
CSC1201: Programming Language 2
2011/11/10: Lecture 21 CMSC 104, Section 4 Richard Chang
Announcements General rules about homeworks
Chapter 6: Functions Starting Out with C++ Early Objects Ninth Edition
Announcements General rules about homeworks
User-defined Functions
FIGURE 4-10 Function Return Statements
Announcements Homework 1 will be assigned this week,
CS149D Elements of Computer Science
Fundamental Programming
CSC1201: Programming Language 2
Functions Extra Examples.
Predefined Functions Revisited
Announcements General rules about homeworks
Functions Imran Rashid CTO at ManiWeber Technologies.
Single-Result Functions & Modularity
FIGURE 4-10 Function Return Statements
Presentation transcript:

Single-Result Functions Section /25/11

Programming Assignment On website.

Reading For Today: Sec. 5.1, pp For Monday: Sec. 4.1 & 4.2, pp

Functions Have discussed math library functions Can create your own functions

Example ch5/circle.cpp – Has a function to find the area of a circle. prototype – declares return-type, name, and parameters function call – name and arguments definition – describes how function computes the value it returns

Single-Result Function Definition // Comment w/ purpose and preconditions resultType functionName( parameter list) // header { // function body Declarations of local variables Statements to find a result Return statement to send back result }

#include //maximum finds the maximum of //three integers, x, y, z //Pre: x, y, and z must be set int maximum(int x, int y, int z); //prototype w/ 3 parameters int main(){ int a,b,c; cout "; cin >> a >> b >> c; cout << "Max is " << maximum(a,b,c)//call with <<endl; //3 arguments return 0; }

//FUNCTION DEFINITION- follows main int maximum(int x, int y, int z)//HEADER { //BODY int max = x; if (y > max)‏ max = y; if (z > max)‏ max = z; return max; }

Arguments and Parameters Arguments are values sent to function Parameters (formal parameters) are dummy variables that receive the information

Arguments and Parameters Must match in type and number – a,b,c & x,y,z – three ints Paired in the order that they appear

Local variables double circleArea(double r) { double a; a = PI * pow(r,2.0); return a; } a is a local variable invisible to main program

return statement double circleArea(double r) { double a; a = PI * pow(r,2.0); return a; } return a Sends value back to call

Boolean Function Returns a bool Can be used in an if statement example – ch5/factor.cpp

Function Write a function that takes a date of birth (mo, da, yr) and today's date(mo, da, yr) and returns a person's age in years. Disregard extra months and days. birthday.cpp has program. We need to add the function.

Question Write an algorithm for a function that takes an exam score and returns a letter grade. Write the function definition Write a main program to test the function. – Called a driver