1 FUNCTIONS - I Chapter 5 ANIMATION. 2 3 Demos Demo of a simple value-returning function Demo of a void function Demo of a void function calling a value-

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
M The University Of Michigan Andrew M. Morgan Andrew M Morgan1 EECS Lecture 03 Savitch Ch. 3-4, Misc More on Functions Memory, Activation Records,
Execute Blocks of Code Multiple Times Svetlin Nakov Telerik Corporation
1 Lab 4 Westfield High School APCS LAB 4 Parameters, apvectors, structs.
How do Methods Work?. Let’s write a method that adds two integer values together and returns the result.
Functions Prototypes, parameter passing, return values, activation frams.
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
G make_counter params: body: count = 0 def counter():... my_counter params: body: count += 1... E1 count0 counter E2E2.
1 Class Vehicle #include #define N 10../.. 2 Class Vehicle class vehicle { public: float speed; char colour[N+1]; char make[N+1];
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
Functions ROBERT REAVES. Functions  Interface – the formal description of what a subprogram does and how we communicate with it  Encapsulation – Hiding.
Functions CS 308 – Data Structures. Function Definition Define function header and function body Value-returning functions return-data-type function-name(parameter.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
Passing arguments by value void func (int x) { x = 4; }... int a = 10;... func(a); cout
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
General Computer Science for Engineers CISC 106 Lecture 30 Dr. John Cavazos Computer and Information Sciences 05/04/2009.
Functions Modules in C++ are called functions and classes
Chapter 7 Functions.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
1 Chapter 9 Additional Control Structures Dale/Weems/Headington.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
CSCI 130 Chapter 5 Functions. Functions are named uniquely Performs a specific task Is independent –should not interfere with other parts of program May.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Functions. Functions are named blocks of code. Functions allow complex programs to be broken down into smaller, simpler tasks. Functions allow commonly.
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.
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.
1 CS161 Introduction to Computer Science Topic #10.
FUNCTIONS (a) Value returning e.g. int main() ….…. return 0; (b) Void (returning) no return statements example To print this message **** ** Welcome.
Chapter 7 Functions CS185/09 - Introduction to Programming Caldwell College.
Tracing through E01, question 9 – step 1 // p02.cc P. Conrad, for CISC181 07S // Exam question for E01 #include using namespace std; void mysteryFunction(int.
1 Command-Line Processing In many operating systems, command-line options are allowed to input parameters to the program SomeProgram Param1 Param2 Param3.
1 Simple Input/Output  C++ offers the iostream library, which defines a system of character-oriented Input/Output (I/O) using object oriented programming.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Lecture 4 Function example. Example1 int max (int a, int b) { int c; if (a > b) c = a; else c = b; return (c); } void main ( ) {int x, y; cin>>x>>y; cout.
Instructor - C. BoyleFall Semester
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Parameters I string ask(string nz) { string thing; cout
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
1 FUNCTIONS - II Chapter 9 and Today we will continue with functions covering…. Passing by Reference  Scope Sorting variables Function Structure.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
1 Introduction to Object Oriented Programming Chapter 10.
 Memory setup  Pointer declaration  Address operator  Indirection  Printing addresses or pointers.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
4.1 Operating Systems Lecture 9 Fork and Exec Read Ch
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
LECTURE 3 PASS BY REFERENCE. METHODS OF PASSING There are 3 primary methods of passing arguments to functions:  pass by value,  pass by reference, 
Programmer-Defined Functions, Call-by-Value, Multiple Files Lab 5
Introduction to Functions
Functions.
Pointers & Functions.
Function Overloading.
CS150 Introduction to Computer Science 1
Example 1 Ask the user to type10 integers of an array T1 and 10 integers of an array T2. Put into T3, an array with 20 integers : the sum of the elements.
Pointers & Functions.
Class: Special Topics Overloading (methods) Copy Constructors
CS150 Introduction to Computer Science 1
Functions Chapter No. 5.
Presentation transcript:

1 FUNCTIONS - I Chapter 5 ANIMATION

2 3 Demos Demo of a simple value-returning function Demo of a void function Demo of a void function calling a value- returning function

3 Demo 1 A Simple Value Returning Function

4 Demo 1) simple float-function float cube(float x) { float y=x*x*x; return y; } int main() { float n=5; cout<< cube(n) << endl; }

5 Demo 1) simple float-function /2 float cube(float x) { float y=x*x*x; return y; } int main() { float n=5;  START EXECUTION cout<< cube(n) << endl; } 5 n

6 float cube(float x) { float y=x*x*x; return y; } int main() { float n=5; cout<< cube(n) << endl;  Fn Call } 5 n 5 x Demo 1) simple float-function /3

7 float cube(float x) { float y=x*x*x;  In Fn return y; } int main() { float n=5; cout<< cube(n) << endl; } 5 x 125 y Demo 1) simple float-function /4

8 float cube(float x) { float y=x*x*x; return y;  RETURN } int main() { float n=5; cout<< cube(n) << endl; } 5 x 125 y Demo 1) simple float-function /5

9 float cube(float x) { float y=x*x*x; return y; } int main() { float n=5; cout<< 125 << endl;  Continue } 5 n Demo 1) simple float-function /6

10 float cube(float x) { float y=x*x*x; return y; } int main() { float n=5; cout<<cube(n)<< endl; }  done 125 Console 5 n Demo 1) simple float-function /7

11 Demo 2 A Void Function

12 void printhello(float x) { for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2);  START HERE } Demo 2) void-function /1

13 Demo 2) void-function /2 void printhello(float x) { for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2);  COPY ARGUMENT TO PARAMETER } 2 x

14 Demo 2) void-function /3 void printhello(float x) { CREATE LOCAL VAR for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2); } 2 x 0 i

15 Demo 2) void-function /4 void printhello(float x) { Check Counter for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2); } 2 x 0 i

16 Demo 2) void-function /5 void printhello(float x) { for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl;  } int main() { printhello(2); } 2 x 0 i Hi There!!! Console

17 Demo 2) void-function /6 void printhello(float x) { Add 1 to counter for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2); } 2 x 0 i Hi There!!! Console

18 Demo 2) void-function /7 void printhello(float x) { Check Counter for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2); } 2 x 1 i Hi There!!! Console

19 Demo 2) void-function /8 void printhello(float x) { for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl;  } int main() { printhello(2); } 2 x 1 i Hi There!!! Console

20 Demo 2) void-function /9 void printhello(float x) { Add 1 to counter for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2); } 2 x 1 i Hi There!!! Console

21 Demo 2) void-function /10 void printhello(float x) { Check Counter for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2); } 2 x 2 i Hi There!!! Console

22 Demo 2) void-function /11 void printhello(float x) { for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } }  Done, go to line after call int main() { printhello(2); } 2 x 2 i Hi There!!! Console

23 Demo 2) void-function /12 void printhello(float x) { for (int i=0; i<x; i++) { cout<< "Hi There !!!!" << endl; } int main() { printhello(2); }  Done Hi There!!! Console

24 Demo 3 Function calling another Function

25 Demo 3) Multiple functions /1 float calc(int l, int w) { return l * w; } void print_area(int len, int wid) { cout<< “Area is:“ <<calc(len,wid); } int main() { print_area(4,5);  START HERE }

26 Demo 3) Multiple functions /2 float calc(int l, int w) { return l * w; } void print_area(int len, int wid) { cout<< “Area is:“ <<calc(len,wid); } int main() { print_area(4,5); Copy args to params } 4 len 5 wid

27 Demo 3) Multiple functions /3 float calc(int l, int w) { return l * w; } void print_area(int len, int wid) { cout<< “Area is:“ <<calc(len,wid);  } int main() { print_area(4,5); } 4 len 5 wid 4 l 5 w

28 Demo 3) Multiple functions /4 float calc(int l, int w) { return l * w;  } void print_area(int len, int wid) { cout<< “Area is:“ <<calc(len,wid); } int main() { print_area(4,5); } 4 len 5 wid 4 l 5 w 20

29 Demo 3) Multiple functions /5 float calc(int l, int w) { return l * w; } void print_area(int len, int wid) { cout<< “Area is:“ <<20; } int main() { print_area(4,5); } 4 len 5 wid 4 l 5 w 20

30 Demo 3) Multiple functions /6 float calc(int l, int w) { return l * w; } void print_area(int len, int wid) { cout<< “Area is:“ <<20;  } int main() { print_area(4,5); } 4 len 5 wid Area is 20 Console

31 Demo 3) Multiple functions /7 float calc(int l, int w) { return l * w; } void print_area(int len, int wid) { cout<< “Area is:“ <<calc(len,wid); }  Done, go to line after call int main() { print_area(4,5); } 4 len 5 wid Area is 20 Console

32 Demo 3) Multiple functions /8 float calc(int l, int w) { return l * w; } void print_area(int len, int wid) { cout<< “Area is:“ <<calc(len,wid); } int main() { print_area(4,5); }  Done Area is 20 Console