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.

Slides:



Advertisements
Similar presentations
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
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.
Chapter 6: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 6: User-Defined Functions I.
Computer Science 1620 Function Scope & Global Variables.
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.
Friday, December 29, 2006 Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration. - Stan Kelly-Bootle.
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
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.
Chapter 6: Function. Scope of Variable A scope is a region of the program and broadly speaking there are three places, where variables can be declared:
Chapter 6: Functions.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
Lecture 9m: Top-Down Design with Functions COS120 Software Development Using C++ AUBG, COS dept.
1 Chapter 9 Scope, Lifetime, and More on Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
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.
Topic 3 – The General Form of a C Program. CISC 105 – Topic 3 The General Form of a C Program Now, all of the basic building blocks of a C program are.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter 6 User-Defined Functions I. Objectives Standard (predefined) functions What are they, and How to use them User-Defined Functions Value returning.
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
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.
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.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
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.
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.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Functions Dr. Sajib Datta Functions A function is a self-contained unit of program code designed to accomplish a particular task. Some functions.
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 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 6: User-Defined Functions I
C++.
BIL 104E Introduction to Scientific and Engineering Computing
Functions Department of Computer Science-BGU יום רביעי 12 ספטמבר 2018.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Global & Local Identifiers
User-Defined Functions
Multiple Files Revisited
6 Chapter Functions.
Namespaces How Shall I Name Thee?.
Chapter 6: User-Defined Functions I
Function.
CS150 Introduction to Computer Science 1
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Department of Computer Science-BGU יום שישי 26 אפריל 2019.
Predefined Functions Revisited
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Eizan Aziz CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING
Function.
Scope Rules.
Presentation transcript:

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 of their C programs. - Robert Firth

§Tutorial §References on website.

bool p; // some statements here if (p) and if (true == p) if (!p) and if (false == p)

double area, circumference, volume, radius=7.5; //circle area = * radius * radius; circumference = 2 * * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * * radius * radius; volume = 4.0/3.0 * * radius * radius * radius; cout<<area<<" "<<volume<<endl;

double area, circumference, volume, radius=7.5; //circle area = * radius * radius; circumference = 2 * * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * * radius * radius; volume = 4.0/3.0 * * radius * radius * radius; cout<<area<<" "<<volume<<endl; Avoid magic numbers

double PI = ; double area, circumference, volume, radius=7.5; //circle area = PI * radius * radius; circumference = 2 * PI * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * PI * radius * radius; volume = 4.0/3.0 * PI * radius * radius * radius; cout<<area<<" "<<volume<<endl;

//Better but still not good enough double PI = ; double area, circumference, volume, radius=7.5; //circle area = PI * radius * radius; circumference = 2 * PI * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * PI * radius * radius; volume = 4.0/3.0 * PI * radius * radius * radius; cout<<area<<" "<<volume<<endl;

const double PI = ; double area, circumference, volume, radius=7.5; //circle area = PI * radius * radius; circumference = 2 * PI * radius; cout<<area<<" "<<circumference<<endl; //sphere area = 4.0 * PI * radius * radius; volume = 4.0/3.0 * PI * radius * radius * radius; cout<<area<<" "<<volume<<endl;

#define PI §At file scope: before main()

Functions l Call by Value

The Black Box Analogy §A black box refers to something that we know how to use, but the method of operation is unknown §A person using a program does not need to know how it is coded §A person using a program needs to know what the program does, not how it does it

Functions and the Black Box Analogy l A programmer who uses a function needs to know what the function does, not how it does it l A programmer needs to know what will be produced if the proper arguments are put into the box

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; cout<<“In main”; answer = sum(sum(5,6),3); cout<<answer; return 0; } int sum (int x, int y){ int z=x+y; cout<<“In sum”; return z; }

Larger of two numbers

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)

#include int isalpha(char c); int isdigit(char c); int isalnum(char c); int ispunct(char c); int isspace(char c); int islower(char c); int isupper(char c); int tolower(char c); int toupper(char c);

Example of local variables int example(int x){ x=54; return x; } int main(){ int x=23; cout<<x<<"\n"; example(x); cout<<x<<"\n"; x=example(x); cout<<x<<"\n"; return 0; }

int example(int x){ x=54; return x; } int main(){ int x=23; cout<<x<<"\n"; //prints 23 example(x); cout<<x<<"\n"; //prints 23 x=example(x); cout<<x<<"\n"; //prints 54 return 0; } return value example

Variables declared outside of all functions: global variables Global variables hold their value throughout the lifetime of your program

Example of global variables int global=100; void example(int x){ global=10*x; } int main(){ cout<<global<<"\n"; global = 200; cout<<global<<"\n"; example(3); cout<<global<<"\n"; example(global); cout<<global<<"\n"; return 0; }

Example of global variables int global=100; void example(int x){ global=10*x; } int main(){ cout<<global<<"\n"; //prints 100 global = 200; cout<<global<<"\n"; //prints 200 example(3); cout<<global<<"\n"; //prints 30 example(global); cout<<global<<"\n"; //prints 300 return 0; }

§Use of global variables can have unexpected effects. §Software engineering principles

§local variables - life of function §global variables - existence of entire execution of program §Global variables are initialized at start of program. They are given a value of zero if no other initialization value is specified. §Un-initialized local variables will have unknown values

Local variables are associated with the block they are declared in. int a=3; if(a<=3){ int x=4; cout<<x; }

§Swap example

What is wrong here? int a=3; if(a<=3){ int x=4; cout<<x; } cout<<x;

Local variables are associated with the block they are declared in. int a=3; if(a<=3){ int x=4; cout<<x; } cout<<x; //Error

int a=10; //global variable int main(){ int a=20; cout<<a<<endl; return 0; }

int a=10; //global variable int main(){ int a=20; cout<<a<<endl; //prints 20 return 0; }

We can use scope resolution operator :: to access global variables.

int a=10; //global variable int main(){ int a=20; cout<<a<<endl; cout<<::a<<endl; return 0; }

int a=10; //global variable int main(){ int a=20; cout<<a<<endl; //prints 20 cout<<::a<<endl; //prints 10 return 0; }

int a=10; int doSomething(){ int a=30; cout<<a<<endl; return a; } int main(){ int a=20; cout<<a<<endl; cout<<::a<<endl; cout<<doSomething()<<endl; return 0; }

int a=10; int doSomething(){ int a=30; cout<<a<<endl; //prints 30 return a; } int main(){ int a=20; cout<<a<<endl; //prints 20 cout<<::a<<endl; //prints 10 cout<<doSomething()<<endl; //prints 30 return 0; }

What will happen here? void example(int f){ int f; f=3; cout<<f; }

What will happen here? void example(int f){ int f; //Error:redefinition of formal parameter f f=3; cout<<f; }

§List of marks §List of names §…

Arrays offer a convenient means of grouping together several related variables Arrays

Declaring an array §type array_name[size]; l allocates memory for size variables l index of first element is 0 l index of last element is size-1 l size must be a constant