Functions.

Slides:



Advertisements
Similar presentations
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
Chapter 7: User-Defined Functions II
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.
CS 201 Functions Debzani Deb.
CHAPTER:09 METHODS(FUNCTIONS) IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
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.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Functions Lecture 4 – Section 2: 9/21/05 Section 4: 9/22/05.
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
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.
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.
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.
CPS120: Introduction to Computer Science Functions.
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.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
CPS120: Introduction to Computer Science Lecture 14 Functions.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Procedural programming in Java Methods, parameters and return values.
Learners Support Publications Functions in C++
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Chapter 6 Functions 6.1 Modular Design A valuable strategy when writing complex programs is to break down the program into several smaller modules. A module.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
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: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 6: User-Defined Functions I
FUNCTIONS IN C++.
CSCI 161: Introduction to Programming Function
Programmazione I a.a. 2017/2018.
Global & Local Identifiers
User-Defined Functions
Implementing Functions from a Detailed Design Quick Tips
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
CSC1201: Programming Language 2
User Defined Functions
User Defined Functions
Functions I Creating a programming with small logical units of code.
Chapter 5 Function Basics
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.
6 Chapter Functions.
Functions, Part 1 of 3 Topics Using Predefined Functions
CPS120: Introduction to Computer Science
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Functions, Part 1 of 3 Topics Using Predefined Functions
CS149D Elements of Computer Science
Dynamic Memory.
CSC1201: Programming Language 2
Functions Imran Rashid CTO at ManiWeber Technologies.
What Is? function predefined, programmer-defined
Pointers: The Basics.
Functions, Part 1 of 3 Topics Using Predefined Functions
Object Oriented programming
Functions I Creating a programming with small logical units of code.
Corresponds with Chapter 5
Presentation transcript:

Functions

Purpose Divide the code into smaller segments that: - Perform one task, isolating the operation - Are Reusable - Internally: within one program - Externally: in other programs - Provide Abstraction: use of a complex object without knowledge of its details.

Syntax Syntax is divided into 2 (optionally 3) parts: 1. Declaration: where the function is defined 2. Invocation: where the function is “called into action” (used). 3. Prototype: where a function is “listed” in a program to allow “invoke before define” when needed (optional).

Declaration Syntax returnType name(formalArguments) { } returnType: is the data type of the value returned, or void when no value is returned. name: is an identifier chosen by the programmer.

Declaration Syntax returnType name(formalArguments) { } formalArguments: is a comma-separated list of 0 or more arguments, where each argument is: type identifier or type & identifier

Declaration Syntax Examples: void hello() { } void print(int x) { } void print(int x, int y, int z){ } float divide(int x, int y) { } string askName() { } void askV(long & a, double & b){ }

Declaration Syntax If a return type is specified, the function must return a value of the specified type. Example: int fun() { int i; ... return i; // required }

Declaration Syntax If the return type is void, the function must NOT return a value. It may use return; if needed. Example: void fun() { ... if (something) return; // optional, but allowed }

Invocation Syntax As an individual statement: name ( actualArguments ); Or, if it returns a value, as part of an expression: int x = name ( actualArguments ) * 2;

Invocation Syntax name ( actualArguments ); actualArguments: a comma-separated list of expressions. Actual Arguments must match Formal Arguments in: - Number - Type - Order

Invocation Syntax Example: void fun(int a, float b, string c){ } fun(2, 3.0, ”Hi”); //valid int a=3; fun(2*a-1, 4, ”?”); //valid fun(2, 3.0); //must have 3 acts fun(2,”Hi”,3.0); //order: int,flt,str int x=fun(2, 3.0, ”Hi”); //reType void

Invocation Syntax When the Formal Argument has a &, the corresponding actual must be a single variable of the exact same type. void fun(int &a) { } int b=2; float c=3; fun(b); //valid fun(b+2); // not just a variable fun(2); // not a variable fun(c); // must be an int variable

Formal Argument Classifications Actual Argument : arg. in the Invocation Formal Argument: arg. in the Declaration - Pass-By-Value (PBV): has no & - Pass-By-Reference (PBR): has a &

Invocation Semantics When a function is invoked: 1. For each PBV (no &) argument: a. Formals are allocated. b. Actuals are evaluated. c. Value of actuals are copied to formals. 2. For each PBR (with &) argument: Formals simply rename corresponding Actuals

Invocation Semantics When a function is invoked (continued): 3. Execution control jumps to the beginning of the function. When the function returns: 1. PBV Formals are deallocated 2. PBR Formals no longer rename Actuals 3. Execution jumps back to the invocation 4. If a value is returned, the invocation represents the value returned.

Semantics Example: execution always begins at main() void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: vars c and d are allocated and initialized void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: function fun() is invoked; follow the Semantics void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: 1.a. PBV formal arguments are allocated void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: 1.b. PBV actual arguments are evaluated: c+2 evaluates to 7 void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: 1.c. PBV actual arg. value copied to Formal: void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: 2. PBR Formals rename Actual: void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: 3. Execution jumps to begin of function void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: a is incremented void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: b is decremented void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics Example: a and b are printed void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics } Example: exit/return: follow semantics void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics } Example: 1. PBV Formals are deallocated void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics } Example: 2. PBR Formals no longer rename void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics } Example: 3. Execution jumps back to Invocation void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics } Example: c and d are printed and program ends void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics } Example: IMPORTANT NOTE: - at the beginning of main: c=5, d=10 - at the end of main: c=5, d=9 - The function changed d, not c, because of & void fun(int a, int & b) { a++; b--; cout << a << ” ” << b << endl; } void main() { int c=5, d=10; fun(c+2,d); cout << c << ” ” << d;

Semantics a = a * 2; return a; } Example 2: Execution starts at main() int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) - 1; cout << d;

Semantics a = a * 2; return a; } Example 2: d is allocated (not initialized) int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) - 1; cout << d;

Semantics a = a * 2; return a; } Example 2: evaluate the Right Side of the = which includes an invocation. int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 1.a. Allocate PBV Formal a int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 1.b. Evaluate PBV actual (to 3) int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 1.c. copy Actual to Formal (essentially: a = 3;) int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 2. There are no PBR Formals int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 3. Execution jumps to { int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: Execute the assignment int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: return the value of a (6) int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 1. PBV Formals are deallocated int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 2. There are no PBR Formals int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 3. Execution returns to the invocation int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics a = a * 2; return a; } Example 2: 4. The Invocation now represents the value returned (6) int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; // 6 - 1 cout << d;

Semantics a = a * 2; return a; } Example 2: Finish the evaluation and assignment int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; // d=5 cout << d;

Semantics a = a * 2; return a; } Example 2: print d and end program int doubleIt(int a) { a = a * 2; return a; } void main() { int d; d = doubleIt(3) – 1; cout << d;

Semantics Example 3: multiple invocations void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl;} void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Execution always starts at main() void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl;} void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Invoke azul() void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl;} void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: No arguments to allocate void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl;} void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Begin execution; print blue void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl;} void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: } means return to invocation void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl;} void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: No value returned, so just continue void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Now invoke pupura() void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: No arguments to set up; just begin execution void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Invoke roja() void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: No arguments to set up, just begin execution void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Print red void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: End function, return to invocation void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Print Purple void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Invoke azul() void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: No arguments to set up; just begin execution void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: Print blue void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: End function; return to invocation void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: End function; return to invocation void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Semantics Example 3: End program void roja() { cout << ”red” << endl; } void azul() { cout << ”blue” << endl; } void pupura() { roja(); cout << ”Purple” << endl; azul(); } void main() { pupura();

Define Before Use! The function Declaration must come before any invocation by any other function int doubleIt(int a) { // declaration ... } void main() { int d; int d = doubleIt(3); // invocation

Define Before Use! ... Otherwise, a compiler error is detected: “Undefined identifier doubleIt” void main() { int d; int d = doubleIt(3); // invocation } int doubleIt(int a) { // declaration ...

Function Prototype Purpose: to describe a function to the compiler without declaring the function. It is a promise to the Compiler: “this function hasn’t been defined yet, but I promise it will be, by the time the Linker is finished!” The promise must include the function’s name, formal arguments, and return type.

Function Prototype Syntax: returnType name ( formalArguments ) ; It is exactly the same as the first line of the Declaration (called the Header), except: - there is a semi-colon at the end - the function’s body ( { ... }) is missing It must appear before any Invocation of the function. Semantics: None. It is simply a message to the compiler on how to translate the program.

Function Prototype Usefulness: 1. to allow functions to be declared after an invocation, as long as the prototype is before the invocation. 2. to allow a source file to invoke a function declared in a different source file (ex: in a library)

Function Prototype Example: int doubleIt(int a); // prototype void main() { int d; int d = doubleIt(3); // invocation } int doubleIt(int a) { // declaration ...

Vocabulary Term Definition Internal Reuse when a function is defined once and invoked many times within a single program External Reuse when a function is defined once and invoked by multiple programs Abstraction use of a complex object without knowledge of its details Function Declaration code that defines a function Function Invocation code that calls a function into action Function Prototype code that describes a function, allowing it to be invoked before it is defined Formal Argument argument found in a function Declaration Actual Argument argument found in a function Invocation Pass-By-Value type of Formal Argument without an ampersand Pass-By-Reference type of Formal Argument with an ampersand