Tuesday, December 26, 2006 There was a most ingenious architect who contrived a new method for building houses, By beginning at the roof and working downward.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

Chapter 3 Top-Down Design with Functions. 3-2 Outline 3.1 BUILDING PROGRAMS FROM EXISING INFORMATION –CASE STUDY: FINDING THE AREA AND CIRCUMFERENCE OF.
Functions a group of declarations and statements that is assigned a name effectively, a named statement block usually has a value a sub-program when we.
Friday, December 22, 2006 When you have a hammer, everything starts looking like a nail. - Abraham Maslow.
Thursday, December 28, 2006 One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination.
Top-Down Design with Functions 4 What do programmer’s (not programs!) use as input to the design of a program? –Documentation Problem definition Requirements.
Programming Functions. A group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a value A sub-program.
Chapter 6: User-Defined Functions I
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: Functions.
A function is a subprogram that acts on data and often returns a value. You are already familiar with the one function that every C++ program possesses:
CP104 Introduction to Programming Top-down design with functions Lecture 6-8 __ 1 Top-Down Design with Functions C Library functions Case studies Top-down.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Programming in C++ Language ( ) Lecture 5: Functions-Part1 Dr. Lubna Badri.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
FUNCTIONS AND STRUCTURED PROGRAMMING CHAPTER 10. Introduction A c program is composed of at least one function definition, that is the main() function.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
111/15/2015CS150 Introduction to Computer Science 1 Summary  Exam: Friday, October 17,  Assignment: Wednesday, October 15, 2003  We have completed.
Procedural Programming Criteria: P2 Task: 1.2 Thomas Jazwinski.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Lecture 2 Friday 11 July Chapter 3, Functions l built-in functions l function prototype, function definition and use l storage class and scope.
Functions Exercise 5. Functions a group of declarations and statements that is assigned a name  effectively, a named statement block  usually has a.
1 10/18/04CS150 Introduction to Computer Science 1 Functions Divide and Conquer.
UniMAP SemI-09/10EKT120: Computer Programming1 Week 5 – Functions (1)
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
KIC/Computer Programming & Problem Solving 1.  Header Files  Storage Classes  Scope Rules  Recursion Outline KIC/Computer Programming & Problem Solving.
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.
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.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
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.
Chapter 3 : Top Down Design with Functions By Suraya Alias.
Functions. Why use functions? They can break your problem down into smaller sub-tasks (modularity).  easier to solve complex problems They make a program.
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.
USING & CREATING FUNCTIONS. MODULAR PROGRAMMING  Why Modular Programming?  Improves Readability & Understandability  Improve Maintainability  Allows.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 Today: Functions. 2 Some General Tips on Programming Write your code modularly Compile + test functionality in the process.
1 Functions Part 1 Prototypes Arguments Overloading Return values.
Simple C Programs.
Chapter 6: User-Defined Functions I
C++.
Predefined Functions Revisited
BIL 104E Introduction to Scientific and Engineering Computing
CMPT 201 Functions.
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
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
Function.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Fundamental Programming
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Predefined Functions Revisited
Eizan Aziz CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Function.
CPS125.
Scope Rules.
Presentation transcript:

Tuesday, December 26, 2006 There was a most ingenious architect who contrived a new method for building houses, By beginning at the roof and working downward to the foundation. - Jonathan Swift, Gulliver’s Travels

Nested Switch char ch1, ch2; cin>>ch1>>ch2; switch(ch1) { case 'A': cout << "This A is part of outer switch\n"; switch(ch2) { case 'A': cout << "This A is part of inner switch\n"; break; case 'B': cout << "This B is part of inner switch\n"; } break; case 'B': cout << "This B is part of outer switch\n"; break; } Equivalent statements?

Nested Switch char ch1, ch2; cin>>ch1>>ch2; switch(ch1) { case 'A': cout << "This A is part of outer switch\n"; switch(ch2) { case 'A': cout << "This A is part of inner switch\n"; break; case 'B': cout << "This B is part of inner switch\n"; } break; case 'B': cout << "This B is part of outer switch\n"; break; } Equivalent statements?

§Top down design

Benefits of Top Down Design §Subtasks, or functions in C++, make programs l Easier to understand l Easier to change l Easier to write l Easier to test l Easier to debug l Easier for teams to develop

§Hardware design l Subparts or modules l Interface §Software design

Three places where variables are declared: 1.Inside functions: local variables 2.In the definition of function parameters: formal parameters 3.Outside of all functions: global variables

local variables are created when function is called and destroyed when function is exited. formal parameters are used like local variables. Their value is lost once the function terminates. They have a special task of receiving the value of arguments. global variables hold their value throughout the lifetime of your program

int sum(int x, int y); int main(){ int answer; cout<<“In main”; answer = sum(2,3); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; }

int sum(int x, int y); int main(){ int answer, num1, num2; cin>>num1>>num2; cout<<“In main”; answer = sum(num1,num2); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; } Function Call Pass by Value

Examples of Functions # include double pi(void); int main(void){ double answer; answer=pi(); cout<<"answer is "<<answer<<endl; return 0; } double pi(void){ double value= ; cout<<"I return value of pi when called"<<endl; return value; }

Examples of Functions I return value of pi when called answer is

#include double area(double radius); double pi(); int main(void){ double answer; answer=area(7.5); cout<<"answer is "<<answer<<endl; return 0; } double area(double radius){ double value=pi()*pi()*radius; return value; } double pi(){ return ; //we can return a constant also }

§Output? answer is

#include double Add2(double d, char c, int i); int main(void){ double dd=4.4; char cc='b'; int ii=15; double ans=Add2(dd,cc,ii); cout<<ans<<endl; cout<<dd<<"\t"<<ii<<"\t"<<cc<<endl; return 0; } double Add2(double d, char c, int i){ d=d+2; i=i+2; c=c+2; cout<<d<<"\t"<<i<<"\t"<<c<<endl; return d; }

§Output? d b §Parameter list order is important!

int main() { int x=30, y=40, z=45, ans1, ans2; x += (++y) * (x/y) + 35*z; x *= (z*98-y)* /z; x /= (34*67*y*(z%x)); ans1 = 3*x+50+y; x=70, y=80, z=85; x += (++y) * (x/y) + 35*z; x *= (z*98-y)* /z; x /= (34*67*y*(z%x)); ans2 = 3*x+50+y; cout<<ans1<<endl<<ans2; }

int calculate(int a, int b, int c); int main(){ int x=30, y=40, z=45, ans1, ans2; ans1 = calculate(x, y, z); x=70, y=80, z=85; ans2 = calculate(x, y, z); cout<<ans1<<endl<<ans2; } int calculate(int a, int b, int c){ int value; a += (++b) * (a/b) + 35*c; a *= (c*98-b)* /c; a /= (34*67*b*(c%a)); value = 3*a+50+b; return value; }

header files predefined functions #include

Math library #include sin(x), cos(x), tan(x), asin(x), acos(x), atan(x), sinh(x), cosh(x), tanh(x), exp(x), log(x), log10(x), pow(x,y), sqrt(x), ceil(x), floor(x)

int main() { int num; double sine_val; for(num=1; num < 100; num++) { sine_val = sin(num*3.14/180.0); cout << num <<" "<<sine_val<< " \n " ; } return 0; }

#include int rand(void)

int sum(int x, int y); int main(){ int answer, x, y; cin>>x>>y; cout<<“In main”; answer = sum(x,y); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; } Function Call Pass by Value