1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs.

Slides:



Advertisements
Similar presentations
Escape Sequences \n newline \t tab \b backspace \r carriage return
Advertisements

1 Lecture 16:User-Definded function I Introduction to Computer Science Spring 2006.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 5 C Functions The best way to develop and maintain a large program is to divide it into several smaller program modules, each of which is more.
Chapter 5 Functions.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Chapter 6: User-Defined Functions I
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
 2007 Pearson Education, Inc. All rights reserved C Functions.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
 2007 Pearson Education, Inc. All rights reserved C Functions.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Methods
Introduction to Methods
Chapter 6: Functions.
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
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 6 Value- Returning Functions and Modules.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 06 (Part I) Functions and an Introduction to Recursion.
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 Introduction to Programming By Engr. Bilal Ahmad 1ITP by Engr. Bilal Ahmad.
Variables and Functions. Open your Encoder program Let’s begin by opening the “Labyrinth Auto Straight” code. Save this file as Labyrinth with variables.
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
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. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
1 Structure of a C Program (continued) Presentation original from Dr. Turner’s class USF - COP C for Engineers Summer 2008.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
COIT29222 Structured Programming 1 COIT29222-Structured Programming Lecture Week 02  Reading: Textbook(4 th Ed.), Chapter 2 Textbook (6 th Ed.), Chapters.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CHAPTER 6 USER-DEFINED FUNCTIONS Made By- Kartik Belwal.
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.
Functions  A Function is a self contained block of one or more statements or a sub program which is designed for a particular task is called functions.
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
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.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
ECE 103 Engineering Programming Chapter 30 C Functions Herbert G. Mayer, PSU CS Status 8/9/2014 Initial content copied verbatim from ECE 103 material developed.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
Chapter 9: Value-Returning Functions
User-Written Functions
Topics Introduction to Functions Defining and Calling a Void Function
Chapter 6: User-Defined Functions I
Deitel- C:How to Program (5ed)
Functions.
User-Defined Functions
Chapter 5 - Functions Outline 5.1 Introduction
Chapter 6: User-Defined Functions I
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
IPC144 Introduction to Programming Using C Week 4 – Lesson 2
Presentation transcript:

1 FUNCTIONS - I Chapter 5 Functions help us write more complex programs

Agenda Introducing Functions Tell me a story– void Functions Parameters and Arguments Random numbers Do the Math– value returning Functions Return statement Prototypes and file layout It’s Logical—bool Functions 2

3 Functions are building blocks… Each box is a “mini-program” of 5-20 lines of code

We use functions… When a task (sequence of code) gets routine and needs to be repeated several times in a program. When we want to hide details of a complicated calculation from the main flow of the program As a tool to break down complicated problems into easily solvable pieces 4

A Simple Function (to begin a story) 5 Function calls Function definition

A function call is a “controlled jump” Program execution always begins in main( ) A function call tells computer to jump into another part of the file (where the definition is) When finished, the computer returns to the line following the function call and continues on thru the program 6

Controlled Jump Illustration 7 Start Once upon a time, Console Output

Function Syntax Rules The function call has the same name as the function definition The function definition must appear before the function call in it’s own separate block of code Curly braces mark the beginning and end of a function definition A function definition cannot be put inside another function definition 8

What the heck is “void”? The first line of a function definition is called the function head or header: void opening( ) The syntax for the function header is: return-type function-name ( parameters ) The word void indicates the function does not return any data when it finishes Part 2 deals with non-void or value-returning functions Notice int main( )? This allows main( ) to return a 0 when it completes successfully. Also, right now, we are not using any parameters 9

Correct Program Layout 10 These are the “building blocks” of code Do not overlap the blocks Main( ) is also a function definition!!

Incorrect Program Layout 11 These Function Blocks are Nested— Syntax Error!!

Parameters and Arguments Information can be given to a function during the function call This allows a variety of function behaviors Makes functions more useful Information is passed from an argument in the function call to a parameter in the definition We’ll modify our program to show this: opening( )  opening(2); // where 2 is an argument We’ll also have to modify the function definition  12

Passing information into a function 13 2 is the argument m is the parameter No matter what argument is given, parameter m get’s a copy of it This morning, Console Output

Other Argument Possibilities 14 This morning, Once upon a time, Once upon a time, This morning, This morning, Console Output Each function call demonstrates a different argument passed to parameter m in opening( )

What is a parameter again? 15 The parameter m receives whatever argument is given in the function call. When the argument is a variable, the value of the variable is given to m Here m in main( ) is different from m in opening( ). Since they live in different functions, they are like separate variables.

The story continues… In the lab, you will get to write your own functions to tell a story And you’ll be able to select a variety of components using a random number generator 16

Random Numbers!! Sometimes we need the computer to pick a number “at random” Very common in video games and computer simulations Requires a different kind of function…one that returns a value so we can use it later. int x; x = rand( ); x now holds a “pseudo” random number between +/-2 billion, for example: , , , , etc 17 Notice: This function call is in an assignment statement

+/- 2 billion? The output of rand( ) is not very useful Too broad a range To make useable, we use modulus (%) to chop it down: Example: 25678%10 = ? %10 = ? %10 = ? Using %10 gives us a random value between 0 and 9 18

To Randomize… Problem: rand( ) by itself always gives the same random number. Solution: Start your program with a call to srand(time(NULL)); This starts the math formula in rand( ) off with a unique #, The number of seconds past Jan 1, 1950 (or so) 19 Plug x in here and now the random number selects the opening

Start your creative juices… Finish Lab4Function1.cpp Next: Part 2, Value Returning functions 20

Before starting Part 2… a)Function Call _______ b)Function Definition_______ c)Parameter_______ d)Argument_______ e)Local Variable_______ f)Return Type_______ 21

22 Part2 Value Returning Functions Useful in math and other operations These functions compute a result and return it For standard math, #include We know that 2 4 = 2*2*2*2 = 16 In C++ we could say pow(2.0, 4); But nothing will be displayed!!!

Show your Result Unlike for void functions, when a math function returns, the original function call is replaced with the result. pow(2.0, 4); To show the result you must either cout or store it in a variable: A) cout<<pow(2.0, 4) ; B) z = pow(2.0, 4); cout<<z ; Notice, 2 arguments BUT Don’t cout a void function: cout<<opening( ); ERROR

For the Math Whiz… 24

25 Standard C++ Library The Standard C++ library is a collection of pre-defined functions which are accessed through header files (such as, etc..) Notice that these pre-defined functions don’t show the processing step (function definition): we do not need to know how the function works, just what it does.

Making your own VRFs When the Standard Library doesn’t have what you need, you can make your own. A value returning function is very similar to a void function with only two small changes: 1.Instead of void before the function name, we put the data type of the return value. 2.At the end of the function definition, we put a return statement to return the result. 26

27 EXAMPLE—Dollar Value Remember Lab 2? We can use a function call to calculate dollars: dollars = dollarValue( nickels, dimes); We are now going to write the definition of the function. But first…

Use the Function Definition Checklist When you are given an example function call, there are a few steps to follow before writing the function definition to make sure your code works. 1.What is the name of the function? ________ 2.What are the parameters it needs (and their type)? __________________________ 3.What is the return-type of the function (the type of the result it computes)?___________________ 4.Write the function header __________________ All of the answers can be determined from the previous slide!!! 28 float dollarValue( int n, int d ) dollarValue Number of nickels and dimes, both integers The variable dollar gets the result and it’s float

Now we can write the definition 29 Type of return value Consistent with return-type value is a “local variable” return-type

30 return… It terminates the execution of the function The function’s return-type specifies the data type of the values that it would return to the calling program Its syntax is : return expression ; where the data-type of the expression value = function's return-type

31 Let’s try another! Define a function named calcSize; the function is passed a character argument representing a size code and the function returns the size in inches (an integer) according to the following chart and sample function calls:

Function Definition Checklist 1.What is the name of the function? ________ 2.What are the parameters it needs (and their type)? __________________________ 3.What is the return-type of the function (the type of the result it computes)?___________________ 4.Write the function header __________________ All of the answers can be determined from the previous slide!!! 32

Now Write the Function Definition 33

34 And another! Write a function that takes arguments for a user’s height and weight, and then computes their hat size according the the following formula: Hat size = 2.9 times the weight in pounds divided by height in inches Here is an example function call: hsize = hat(weight, height); Here is some example test data: weight=150, height=70  hsize=6.2

Function Definition Checklist 1.What is the name of the function? ________ 2.What are the parameters it needs (and their type)? __________________________ 3.What is the return-type of the function (the type of the result it computes)?___________________ 4.Write the function header __________________ All of the answers can be determined from the previous slide!!! 35

Now Write the Function Definition 36

Bool Functions for Logic A bool function is a special type of VRF bool data type has only two values: true, false This means a function that returns a bool value can be used in an if-statement  37

A bool function example Suppose you want to check if n is between 0 and 100. You could say Or you could put the condition in a bool function: And use that function instead of the boolean logic 38

39 Time to explore on your own… Finish Lab4Function2.cpp Practice will help you master this topic Recommend you solve one more function from the Assignment 4 handout