CPS120: Introduction to Computer Science Functions.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Chapter 5 Functions.
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.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
11/1/06 1 Hofstra University, CSC005 Chapter 8 (Part 3) High Level Programming Languages.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 6: Functions by.
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Chapter 6: Functions.
Chapter 7 Functions.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
Flow of Control. 2 Control Structures Control structure: An instruction that determines the order in which other instructions in a program are executed.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
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.
Chapter 6: Functions Starting Out with C++ Early Objects
Chapter 06 (Part I) Functions and an Introduction to Recursion.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
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.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
1 CS161 Introduction to Computer Science Topic #9.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 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.
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.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Functions Starting Out with C++ Early Objects Eighth Edition.
Functions Chapter 6. Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules Function: a collection.
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.
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.
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.
CSIS 113A Lecture 5 Functions. Introduction to Functions  Building Blocks of Programs  Other terminology in other languages:  Procedures, subprograms,
CPS120 Introduction to Computer Science Exam Review Lecture 18.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
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.
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 Functions Part 1 Prototypes Arguments Overloading Return values.
FUNCTIONS Source of Notes: Mr Jamal Othman
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Predefined Functions Revisited
Function There are two types of Function User Defined Function
CSCI 161: Introduction to Programming Function
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
User Defined Functions
Chapter 4 void Functions
Functions A function is a “pre-packaged” block of code written to perform a well-defined task Why? Code sharing and reusability Reduces errors Write and.
Functions.
6 Chapter Functions.
Functions.
Group Status Project Status.
CPS120: Introduction to Computer Science
Flow of Control.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Predefined Functions Revisited
Standard Version of Starting Out with C++, 4th Edition
Presentation transcript:

CPS120: Introduction to Computer Science Functions

Subprogram Statements We can give a section of code a name and use that name as a statement in another part of the program When the name is encountered, the processing in the other part of the program halts while the named code is executed -- Invocation

Subprogram Statements There are times when the calling unit needs to give information to the function to use in its processing A parameter list is a list of the identifiers with which the subprogram is to work, along with the types of each identifier placed in parentheses beside the function name

Subprogram / Function Statements Subprogram flow of control

Subprogram Statements Subprogram flow of control

Subprogram Statements Parameters: The identifiers listed in parentheses beside the function name; sometimes they are called formal parameters Arguments: The identifiers listed in parentheses on the function call; sometimes they are called actual parameters

Subprogram Statements Value parameter: a parameter that expects a copy of its argument to be passed by the calling unit (put on the message board) Reference parameter: a parameter that expects the address of its argument to be passed by the calling unit (put on the message board)

Subprogram Statements

Functions Every C++ program must have a main function Each of the smaller tasks (let's say, subtasks) can be coded as C++ functions that go together with the to make up a structured program.

Functions & Structured Programs Using functions makes it easier to code programs. Using functions also makes it easier to debug large programs. Using functions makes it easier to maintain and upgrade a program after the first version has been finished

Guidelines for Building Programs With Multiple Functions Organization: - programs with functions are easier to read and modify Autonomy:- functions should not depend on data or code outside of the function any more than necessary Encapsulation: - functions should keep all of their "details" to themselves Reusability: functions may be reused in other programs or, even, by other programmers

Function Syntax Functions are given valid identifier names, just like variables and constants. A function name must be followed by parentheses Coded functions to the bottom of a C++ program, after the main function. If the functions are listed after the main function, then function prototypes must be included at the top of the program, above the main function An error will definitely result if you call a function from within the main function that has not been prototyped.

Structure of Functions in C++ function-name(argument list) argument declaration; { local variable declarations; executable statement1; executable statement2; return (expression); }

An Example of A Function #include void printMyMessage(int numOfTimes); // PROTOTYPE and NAME int main( ) { int userInput = 0; cout << "Enter a number between 1 and 10 (0 to Exit): " ; cin >> userInput; if (userInput != 0) { printMyMessage (userInput); // CALL STATEMENT WITH ACTUAL PARAMETER } else cout << "Thanks, Bye!"; return 0; } // end of main void printMyMessage(int numOfTimes)// FUNCTION HEADER WITH RETURN TYPE AND // ACTUAL PARAMETER { int i=0; // LOCAL VARIABLE WITHIN THE FUNCTION for (i=0; i<= numOfTimes; i++)// BODY {cout << "Let's Go State!!" << endl;}// OF THE } //end of printMyMessage// FUNCTION

Describing a Function The first line of a function is called the function header Before the name of the function you must specify a "return type." The return type is the data type of the value that is returned by the function to the calling function If the function does not return any value, you must type the word void as the return type After the name of the function in the function header, you must include a parameter list. Immediately preceding each parameter, you must identify the data type of that parameter.

Returning Values If the function is not a void function, there must be a return statement at the end of the function

Scope of Variables The scope of a variable is the area in which it can be legally referenced Variables are either global or local in nature Global variables are ones that are declared outside and above the main function They can be used in any function throughout the program. It is not wise to use global variables any more than you have to. Local variables are ones that are declared inside of a function, including main. They cannot be used or referred to in other functions

Using Functions Avoid using cin or cout statements within functions Unless the whole purpose of a function is to obtain (and or validate) a user's input, you should not use a cin statement within a function (besides main). Unless the whole purpose of a function is to display something such as a menu, you should avoid using a cout statement within a function besides main.

Passing Data Data is passed to functions as arguments When a function is "called" by the main function one or more arguments are passed to the function On the receiving end, the function accepts these arguments The variable names of the arguments from the "calling" function do not have to be the same as the names in the "called" function. The data types of the arguments and the parameters should match exactly

More About Passing Arguments There are technically three ways to pass data as arguments to functions 1. passing by value is the preferred method. You simply use a variable name, an actual numeric literal, or an expression in the parentheses of the call statement 2. passing by reference is to be used when you want the function to actually and permanently change the values of one or more variables You must use an ampersand (&) before the formal parameter names in the function header (the first line of the function definition) to denote passing by reference 3. passing by address is technically what happens when you pass an array to a function

Another Look at Return Values Often, though, you want your function to return a computed value to the calling function It is not possible in C++ to execute two return statements within a function It is not possible to return two values in the same return statement

Library Functions There are many functions available to C++ programmers which were written by other programmers Use the #include compiler directive at the top of your program to take advantage of these functions. To use the you do not even have to know exactly how they work You do have to know how many arguments to send to the functions and what datatypes to use for those functions.

Recursion Recursion: the ability of a subprogram to call itself Each recursive solution has at least two cases base case: the one to which we have an answer general case: expresses the solution in terms of a call to itself with a smaller version of the problem For example, the factorial of a number is defined as the number times the product of all the numbers between itself and 0: N! = N * (N  1)!