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.

Slides:



Advertisements
Similar presentations
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Advertisements

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Computer Science 1620 Functions. Given a number n, the factorial of n, written n!, is computed as follows: note: 0! = 1 examples: n! = n x (n-1) x (n-2)
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.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS 201 Functions Debzani Deb.
Chapter 6: User-Defined Functions I
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
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.
11 Chapter 5 METHODS. 22 INTRODUCTION TO METHODS A method is a named block of statements that performs a specific task. Other languages use the terms.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 6 Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: 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.
CPSC 230 Computers and Programming I Spring 2003 Dr. Lynn Lambert.
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.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 4 Introduction to Classes, Objects, Methods and strings
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Lecture 10: Modular Programming (functions) B Burlingame 13 April 2015.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
1 Brief Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Brief Edition Chapter 6 Functions.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
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 : Top Down Design with Functions By Suraya Alias.
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.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
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 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
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.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Bill Tucker Austin Community College COSC 1315
Chapter 9: Value-Returning Functions
Chapter 6: User-Defined Functions I
Lesson #6 Modular Programming and Functions.
Lesson #6 Modular Programming and Functions.
CSCI 161: Introduction to Programming Function
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
6 Chapter Functions.
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
In C Programming Language
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Imran Rashid CTO at ManiWeber Technologies.
Chapter 10: Void Functions
Standard Version of Starting Out with C++, 4th Edition
CPS125.
Presentation transcript:

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 a new number on your phone, view a text message or get cash at an ATM. In the case of storing a new number on the phone, you must provide the information to be stored (inputs). The phone then stores that information so that it can be retrieved at another time. When you view a text message you request that a specific message be displayed (input) and the phone presents the message to you on the screen (output). When you get cash at an ATM you have to tell the machine how much you want and may need to provide it with information about which account you want to draw from (inputs). The machine then performs some tasks that you don’t see such as making sure the account has sufficient funds and subtracting the withdrawal from the account to create a new balance. It then either gives you an error message (in the case of insufficient funds) or dispenses the requested amount of cash (outputs).

C++ Functions Like the previous examples, functions in C++ may or may not have inputs and outputs but they all perform a very specific task or tasks. Syntax for function definition: function_return_type function_name (parameter list) { C++ code needed to implement the task the function is to perform return return_value; } The parameter list indicates the types that must be used when calling the function and the order of the variables. Note-1: The parameter list may be empty or contain multiple parameter. Note-2: The first line above is called the function ‘header’

Functions You have already been exposed to some functions in the ‘cmath’ library (#include : double pow(double base, double exponent) where x is the number being raised to the y power double sqrt(double x) where x is the number for which the square root is to be calculated The red ‘pow’ and ‘sqrt’ are the names of the functions base, exponent and x in the above statements are called the parameters of the functions. (Note: Sometimes they may be called arguments. In this class we will distinguish between the term argument and parameter) The blue double indicates the data type of the parameters. The purple double is the data type of the value that is returned by the functions (the return type).

Calling Functions Examples: You ‘called’ these functions in a fashion similar to that below double p, q, r, s; p = 10; q = 3; r = 25; s = pow(p,q); cout << “s “ << s; // outputs s = 10 3 or 1000 s = sqrt(r); cout << “s “ << s; // outputs s = 5 Note: p and q above are the arguments substituted for the corresponding location of the parameters in the function definition.

What is Not Seen? What you don’t see is the code that is executed when you invoke (call) the library functions. This code is stored in the cmath library that you include via the #include preprocessor directive Note that each of these two functions perform a single task. These functions are in a library because they perform tasks commonly used by many programs. Someone had to write the code as they are not a part of the core C++ language. Programmers can define their own functions that perform a task that the program they are writing can use.

Defining a Function Syntax/Structure (revisited) return_type function_name(parameter1_type arg1, parameter2_type parameter2, …) { code to perform the task of the function (This includes any variable definitions that it may need) return value (not needed for all functions) } Note: If the task a function performs does not need to return a value (like pow and sqrt) it still has to have a defined ‘return type’. The return type of a function that simply performs a task but does not return a value is of type ‘ void ’.

Function CS1428 Style Requirements Function names should provide an indication of the task the function performs Function names should have each word start with an upper case letter and the rest lower case. Words should NOT be separated with an underscore Example: For a function that accepts data in its parameters and returns ‘true’ if the data is valid and ‘false otherwise, you might have a name like bool IsValid(int number) where IsValid is the function name and number is a parameter of IsValid. Each function definition will have a header comment with ** that go entirely across the page

Where Does the Function Definition Go? The compiler has to know a few things about a function before it can compile (translate) a piece of code that calls the function. There are two ways this can be accomplished. 1.The function must defined in the source code file prior to any other piece of code calling that function. or 2.A function prototype can be used to bypass the above requirement. Style Note: Each function should have a header comment with ****** all the way across the top of the comment to visually separate the function definitions. The header should contain the function name, its inputs (parameters) and output (return value) as well as a description of the task(s) it is to perform.

What Is a Function Prototype? A function prototype is like a function declaration statement. It includes the return type of the function, the function name and the types of any parameters. Ex. void displaymenu (); //Note: no parameters int biggest (int, int, int); //Note: three parameters The prototype (function declaration) statements must be ABOVE any other function that calls that function.

Functions Returning a Value Functions with a return type other than void can be used to provide data to the calling function. Let’s look at our void function DisplayMenu and extend the task of the code in the function to include getting the user’s menu choice and returning it to the calling function.

Functions with Parameters *Sending Information TO a Function* Data can be sent to a function so that the function can use the data to complete its task. Such data are called parameters/arguments. In the function definition we refer to them as parameters. In the function call we refer to the values inserted in place of the parameters as arguments. Functions can have multiple parameters In the function definition the data types of the parameters must be declared When the function is called, only the name of the argument being sent is included. Its data type of an argument must match the type of the corresponding parameter in the function definition. The scope of variables defined within a function are limited to that function. Only the value of the argument is passed, not its memory location (Called ‘Pass by Value’)

Example: Parameters vs. Arguments /****************************************************************************** * AreaTriangle – given the base and height of a triangle, this function * returns the area */ parameters double AreaTriangle(double base, double height) { double calculated_area; calculated_area =.5 * base * height; return calculated_area } corresponds to CALLING CODE SNIPPET: double tri_area, tri_base, tri_hgt; tri_base = 24.6; tri_hgt = 7.8; tri_area = AreaTriangle(tri_base, tri_hgt); argument s