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.

Slides:



Advertisements
Similar presentations
User Defined Functions
Advertisements

Programmer-defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc.
Functions  Programmer-Defined Functions  Local Variables in Functions  Overloading Function Names  void Functions,  Call-By-Reference Parameters in.
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.
An Introduction to Programming with C++ Fifth Edition
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
CS 201 Functions Debzani Deb.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: Functions.
Chapter 4 Procedural Abstraction and Functions That Return a Value.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
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.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Iterative Constructs Review l What are named constants? Why are they needed? l What is a block? What is special about declaring a variable inside a block?
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
CPS120: Introduction to Computer Science Functions.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
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.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
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.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
The Preprocessor Directives Introduction Preprocessing – Occurs before program compiled Inclusion of external files Definition of symbolic constants.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
Functions Procedural Abstraction Flow of Control INFSY 307 Spring 2003 Lecture 4.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
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 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
Functions.
What Is? function predefined, programmer-defined
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Predefined Functions Revisited
Iterative Constructs Review
CMPT 201 Functions.
FUNCTIONS IN C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Chapter 5 - Functions Outline 5.1 Introduction
Multiple Files Revisited
Expression Review what is the result and type of these expressions?
User Defined Functions
6 Chapter Functions.
Iterative Constructs Review
Chapter 9: Value-Returning Functions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
COMS 261 Computer Science I
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CS 144 Advanced C++ Programming January 31 Class Meeting
What Is? function predefined, programmer-defined
Presentation transcript:

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 what is the type of arguments and the type of return value and why are they important? l what is include directive and how is it related to predefined functions? linker? l what are type changing functions and why are they needed? l what is type casting? what does function time() do? what do functions rand() and srand() do? What is a seed? Why is seed important for random number generator? what is RAND_MAX ? How does one get a random number in a desired range? Predefined Functions Revisited 1

Programmer-Defined Functions

l Functions are named portions of code l Two types of functions: n predefined - provided in libraries for the benefit of all programmers n programmer-defined - written by programmer l To carry out its task the function accepts arguments l To use a function the programmer writes the function name and a list of arguments for the function to use. This is called function call (or function invocation) l every function returns a result in a return value l a function call can be used in any place an expression or statement is used n if a function is used as an expression - the function evaluates to its return value n if a function is used as a statement - the return value is ignored l arguments and return value of a function are of specified type Functions 3

cout << add1(4+5)+6 << endl; Function Invocation l caller – function that invokes another l callee – function that is being invoked l processing a function invocation: caller is suspended, callee is executed, callee computes the return value which substitutes invocation in the caller n if invocation is inside an expression, the return value is used to evaluate the expression argument invocation 4

l function definition – specifies instructions that the function executes l consists of head, body Function Definition function body return statement parameter return type function name function head double circleArea (double r) { const double PI = ; return PI * r * r; } 5

l programmer defined function cannot know what arguments will be passed to it; it uses (formal) parameters l parameters - local variables of the callee that are initialized to the value of arguments at invocation l return-statement specifies what value the function returns: return expression; n the expression is evaluated, converted to the type specified in function head (types should match) and function terminates n a return-statement is optional. If a function does not have a return- statement it terminates when the last statement is executed. The returned value is unspecified n it is possible to have multiple return-statements. However, putting a return someplace deep in function code is bad style. Try to code a single return or obvious returns. Parameters and Return-statement 6

l function prototype - quickly introduces the function returnValue functionName(type parameterName,…,); –expanded form – mentions parameter types and names: names are optional but sometimes desirable for clarity int add1(int i); –abbreviated form – mentions only parameter types int add1(int); l before function invocation, the compiler needs to see either its definition or prototype l unlike variables the function declarations and function prototypes should be put outside any other function: no nested functions in C++ standard l it is (almost) possible to write a program without prototypes: put function definitions before invocations n such program is hard to understand: more detailed functions would be ahead in file appropriate program style - put function prototypes first, then main() then other functions with increasing level of detail Function Prototype 7

l treat a function prototype as a variable definition: append a short inline description of the function l precede a function definition with at least one line of comments explaining the purpose of the function, possibly comment on parameters Commenting Functions 8

What are the terms for the constructs in gray boxes? 9 #include double circleArea(double r); // computes circle area // manage circle computation int main() { cout << "Enter radius: "; double MyRadius; // circle radius cin >> MyRadius; double Area = circleArea(MyRadius); cout << "Circle has area " << Area; } // computes area of radius r circle double circleArea(double r) { const double PI = ; return PI * r * r; } Function Terms Quick Review

Simple Program Structure l include statements l using statements l function prototypes l function definitions n main() n rest with increasing order of detail 10

variable that is declared inside a function is local: it cannot be used outside of the function l the scope of such variable is from declaration till function end l parameters are also local variables l local variables of two different functions are different even if they have the same name note that variables declared in main() are local to main() example // computes sum of integers in a...b int sum(int a, int b) { int total = 0; // this is a local variable for (int i = a; i <= b; ++i) { total += i; } return total; } Local Variables 11

l the same constants may be used in multiple functions. l if a constant is declared outside any function it is called global and it can be used (its scope is) anywhere in the program from the place it is declared const double pi = ; const double taxRate = 0.05; // 5% sales tax l similarly one can declare global variables. Global variables can be used and modified in any function of the program: int errorcode = 0; n using global variables makes your program hard to understand and debug and should be avoided Global Constants and Variables 12

Call-by-Value l formal parameters are local variables of the function l when the function is called the values of the arguments are evaluated and assigned to respective parameters l as any local variable, the value of a parameter can be changed in a function l this change does not affect the values of the original arguments l such discipline of parameter passing is called call-by-value int add1(int i) { ++i; return i; }... int n=5; int newn = add1(n); // value of n is unchanged 13