1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
1 CS 105 Lecture 11 Arrays Version of Wed, Apr 6, 2011, 6:20 pm.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1 ; Programmer-Defined Functions Two components of a function definition.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Chapter 6: User-Defined Functions I
Overview creating your own functions calling your own functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
Chapter 6: User-Defined Functions I
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 10/30/06CS150 Introduction to Computer Science 1 Functions Chapter 3, page 313.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Functions A function is a snippet of code that performs a specific task or tasks. You use a multitude of functions daily when you do such things as store.
Chapter 7 Functions.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
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 Why we use functions C library functions Creating our own functions.
CHAPTER 5 FUNCTIONS I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
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.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
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.
Function prototype A function must be declared before it can be referenced. One way to declare a function is to insert a function prototype before the.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Programming Fundamentals Enumerations and Functions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
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.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6: User-Defined Functions I
CSCI 161: Introduction to Programming Function
User-Defined Functions
6 Chapter Functions.
File Review Declare the File Stream Object Name
Chapter 6: User-Defined Functions I
CS149D Elements of Computer Science
Fundamental Programming
Functions Imran Rashid CTO at ManiWeber Technologies.
Presentation transcript:

1 CS 105 Lecture 10 Functions Version of Mon, Mar 28, 2011, 3:13 pm

2 Quiz 2 Scores Hmm.... Grade Range50Count Below 204 Total17 17 Scores: Avg 26.3/50 (or 52.6/100)Std dev 11.7

3 Quiz 2 Scores Numerous problems came from Labs Q2 QuestionPtsTextbook Exercise 124.6(a): switch 634.8: switch 735.2: while 835.9: while 935.6: for loop 1015 PgmEx 5.10(c) Total29

4 Functions Function: A discrete piece of code that performs a specific operation or task Named with a descriptive identifier Called from main() or another function When called, program control (execution) is transferred to the function. Function performs required tasks, and then possibly returns a value. After return from function, control returns to the statement following the function call.

5 Function Attributes Function Name: Identifier used to call function. Function Parameter(s) or Argument(s): value(s) passed into function for use by function code. Function Return Value: Value returned by function back to calling function.

6 Math Library Provides a library of common math functions. Get via #include Must call functions with correct number and order of parameters. Functions return value after calculations. Example: The power function pow; square root function sqrt. double bigValue; double base = 2.0, exponent = 20.0; bigValue = pow(base, exponent); cout << bigValue << " " << sqrt(bigValue) << endl;

7 User-Defined Functions We can write our own functions; we need to specify the name of the function, the number and kinds of parameters it takes, and the kind of value it returns (if any). Plus, the "body" of the function — the code that does the calculation Example: double calcAverage(int total, int numItems) { double result = (double) total / numItems; return result; }

8 Define Function Before Main #include using namespace std; double calcAverage(int total, int numItems) { double result = (double) total / numItems; return result; } int main() { int sumGrades = 921, numStudents = 10; double avgGrade; avgGrade = calcAverage(sumGrades,numStudents); cout << avgGrade << endl; cout << calcAverage(1200, 14) << endl; }

9 Define Functions After Main? If you have many functions, then you might have a lot of code to get through before you actually get to the main program. Nice to have the main program first and the functions afterward. But what if we use calcAvg before we give its definition? How will the compiler know if we're calling calcAvg correctly?

10 A function prototype tells the compiler how we'll use the function. (Omits the function body.) Use Function Prototype #include using namespace std; Function Prototype "declares" calcAverage double calcAverage(int total, int numItems); ←––––––  int main() { // etc } "Definition" of calcAverage gives its body double calcAverage(int total, int numItems) { // rest of body as before }

11 Declaring a Function Function Prototype: Declares a function's name and how it is called; it doesn't define the body of the function. Used by the compiler check for syntax errors in how we call the function Example: double calcAverage(int total, int numItems); double is the return type. calcAverage is the function name. total is the first parameter and it has type int numItems is the second parameter and it has type int. Standard functions (e.g., those in the STL = Standard Template Library) have function prototypes in header files that we #include.

12 Functions – Return Types A function with a return type of int must have a return of some integer. Similarly, a function with a return type of double must have a return of some double, etc. That's why main programs include return 0; A function with void as its return type doesn't return a value. (A.k.a. "void" function) It can have a return; statement (with no value). Or it can fall off the end of the function (a return; is assumed).

13 Parameters & Arguments May pass as many parameters as necessary to function. A copy of the value of the parameter is passed to the function. Changing the value of the parameter in the function does not affect the value of the original variable. This is called Pass-by-Value

14 Example of void function #include using namespace std; void printNum(int); // function prototype int main() { int myNumber = 7; printNum(myNumber); // function call printNum(9); return 0; } // begin function definition void printNum(int numToPrint) { cout << numToPrint; }

15 Functions Write a function that accepts one integer as a parameter. The function returns the sum of the integers from 1 to the integer passed to the function. 5 minutes …… GO!

16 Sum Function int sumOfInt(int num) { int i, sum = 0; for(i = 1; i <= num; i++) sum = sum + i; return(sum); }

17 Function Calls Write a function call to call sumOfInt with an argument of 5, save the return value into a variable and print it out. 2 minutes – GO!

18 Function Calls int sumOfInt(int); int main() { int sum5; sum5 = sumOfInt(5); cout << sum5 << endl; return 0; }

19 Display Line Function Write a function that displays a character some number of times to the display. The function is passed the character and the number of times to display it. 5 minutes: GO!

20 displayChar() Function void displayChar(char charPassed, int times) { int i; for(i = 0; i < times; i++) cout << charPassed; cout << endl; }

21 do { theLabs(threeHoursAWeek); } while (!semesterDone);