1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.

Slides:



Advertisements
Similar presentations
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Advertisements

C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
 2006 Pearson Education, Inc. All rights reserved Functions.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 7: User-Defined Functions II.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
FunctionsFunctions Systems Programming. Systems Programming: Functions 2 Functions   Simple Function Example   Function Prototype and Declaration.
Computer Science 1620 Function Scope & Global Variables.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
FunctionsFunctions Systems Programming Concepts. Functions   Simple Function Example   Function Prototype and Declaration   Math Library Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Chapter 6: User-Defined Functions
C Functions Pepper. Objectives Create functions Function prototypes Parameters – Pass by value or reference – Sending a reference Return values Math functions.
Learners Support Publications Classes and Objects.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Lecture 13: Working with Multiple Programmers. Headers Header files: Each standard library has a corresponding header. The function prototype for all.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Dale Roberts CSCI 230 Functions Scope, Parameter Passing, Storage Specifiers Department of Computer and Information Science, School of Science, IUPUI Dale.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
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.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
1 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Functions Skill Area 314 Part B. Lecture Overview Functions Function Prototypes Function Definitions Local Variables Global Variables Default Parameters.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 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.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Functions.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
IS Program Design and Software Tools Introduction to C++ Programming
A Lecture for the c++ Course
Chapter 5 Functions.
FUNCTIONS IN C++.
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals Lecture #7 Functions
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
6 Functions.
Scope, Parameter Passing, Storage Specifiers
Value returning Functions
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.
Arrays Kingdom of Saudi Arabia
Scope Rules and Storage Types
Function “Inputs and Outputs”
Variables have attributes
The Function Prototype
Functions Chapter No. 5.
Presentation transcript:

1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading

2 Function essentials C++ library functions Include appropriate header file Call function by name #include using std::cout; using std::endl; int main() { double num = 2.0; cout << "The square root of " << num << " is " << sqrt(num) << endl; return 0; } library of math functions argument (actual parameter)

3 Function essentials User-defined functions Declare a function (= write a function prototype to specify its name, number and types of parameters, type of return value) Define a function (= write the body of the function) Call function by name Syntax return_type function_name (argument_list) ; default is int A list of the types and names of the function's input arguments. Looks like a comma-separated list of variable declarations. Where? Between the preprocessor directives and main, or in header file

4 Function essentials #define PI #include double compute_circle_area(double radius); int main () { double radius, area; std::cout << "Enter the radius in inches: "; std::cin >> radius; area = compute_circle_area(radius); std::cout << "The area is " << area << " inches" << std::endl; return 0; } double compute_circle_area (double radius) { return PI * radius * radius; }

5 Scope Scope of an identifier = the portion of a program where the identifier may be referenced. File scope An identifier declared outside any function has file scope. This means that it can be referenced anywhere within the file. Example 1 : function prototypes Example 2 : global variables Usually declared before main() They may be accessed anywhere in the program Avoid using global variables! They may allow unintended side effects to occur.

6 Example: file scope #include double num; double square(); int main () { num = 2.5; double num_sq = square(); return 0; } double square() { double result = num * num; return result; } Scope of num

7 Scope Block scope An identifier declared within a block (sequence of statements enclosed in curly braces) has block scope. This means that it can be referenced only within the block. Example : local variables Two functions may have local variables with the same name without conflict, since the variables exist in different scopes.

8 Example: block scope #include double square(double); int main () { double num, num_squared; num = 2.5; num_squared = square(num); return 0; } double square(double x) { double num_squared; num_squared = x * x; return num_squared; } Scope of num, num_squared Scope of num_squared completely different variables

9 Example: block scope #include int main () { int x; { int y = 20; } x = y * 2; return 0; } Scope of x Scope of y ERROR! y is out of scope (not visible)

10 Scope Function-prototype scope This applies to the parameters of a function. They are visible only within the function. Other types of scope will be discussed later on. Scope conflicts: If two variables have the same name and their scopes overlap, then the name in the inner scope hides the name in the outer scope. Avoid using duplicate identifiers in a program!

11 Example: scope conflicts #include using std::cout; using std::endl; float num = 10.0; void print_num() { cout << num << endl; } int main () { int num = 5; cout << num << endl; print_num(); return 0; } scope of global num Scope of local num Program output: 5 10 DANGEROUS CODE! DO NOT DO THIS

12 Example: scope conflicts #include using std::cout; using std::endl; int num = 5; void print_magic_num() { int num = 20; cout << num << endl; } int main () { cout << num << endl; print_magic_num(); cout << num << endl; return 0; } scope of global num Scope of local num Program output: The local num hides the definition of the global num

13 Example: scope conflicts #include using std::cout; using std::endl; float num; void print_num() { cout << num << endl; } int main () { num = 13.5; cout << num << endl; int num = 5; cout << num << endl; print_num(); return 0; } scope of global float, num Scope of local int, num Program output: The local num hides the definition of the global num DANGEROUS CODE! DO NOT DO THIS

14 Storage class Storage class of an identifier = the period during which the identifier exists in memory automatic storage The identifier exists only during the execution of the block in which it is defined. This is the default storage for local variables. Storage class: WHEN Scope: WHERE

15 Storage class static storage The identifier exists from the moment the program begins execution. The identifier is bound to storage as the program is compiled. This can allow the value of a local variable to be retained across calls. The local variable must be declared with the keyword 'static' Global variables have static storage.

16 Example: static #include void increment(); int main () { for (int i=0; i<5; i++) increment(); return 0; } void increment() { static int num = 1; std::cout << num++ << std::endl; } Program output:

17 extern If a variable is declared (as a global) in file A and used in file B, the extern keyword is used in file B to tell the compiler that the variable is declared elsewhere: // file proj1.cpp int num_students = 14; // file proj2.cpp void get_class_size () { extern int num_students; cout << num_students; }

18 static vs. extern static is often used when we do not want to allow other files to modify a variable. // file proj1.cpp static int num_students; // num_students can be // used by any function // inside proj1.cpp, but // not outside it. // file proj2.cpp void set_students () { extern int num_students; num_students = 14; } ERROR! There is no non-static global variable by that name.

19 Stack frames A function needs to save information in memory during its execution. For example, it needs to save its local variables and its parameters. This information is grouped together in an area called a "frame" (or "activation record") Storage is organized as a stack (LIFO structure) The stack contains a frame for each active function When a function is called, a new stack frame is created for it and pushed onto the stack. When the function exits, its stack frame is popped.

20 Parameter passing Call by value The caller evaluates the actual parameters and passes copies of their values to the called function. Changes to the copies do not affect the values of the original variables. Call by reference The caller supplies the address of the actual parameter rather than a copy of its value. This allows the caller to modify the values of the original variables. It is often used when we want to avoid copying "large" data. Using the keyword 'const' allows us to pass values by reference without letting the called function modify them.

21 Example: Call by value #include using std::cout; using std::endl; void print_num (int num); int main () { int x = 4; print_num(x); return 0; } void print_num(int num) { cout << num << endl; } The value of x is copied into num. Program output: 4

22 Example: Call by value #include using std::cout; using std::endl; void update_num (int num); int main () { int num = 4; print_num(num); cout << num << endl; return 0; } void update_num(int num) { num++; cout << num << endl; } Completely different variables that happen to have the same name. They exist in different scopes, so there is no conflict. The value of num is copied into num. The modification of num inside update_num() has no effect on the num defined in main. Program output: 5 4

23 Example: Call by value #include using std::cout; using std::endl; void swap (int a, int b); int main () { int num1 = 4, num2 = 12; swap(num1, num2); cout << num1 << endl << num2 << endl; return 0; } void swap (int a, int b) { int temp = a; a = b; b = temp; } Program output: 4 12 Since num1 and num2 were passed by value, the changes to a and b had no effect on num1 and num2. The numbers have not been swapped.

24 Example: Call by reference #include using std::cout; using std::endl; void swap (int& a, int& b); int main () { int num1 = 4, num2 = 12; swap(num1, num2); cout << num1 << endl << num2 << endl; return 0; } void swap (int& a, int& b) { int temp = a; a = b; b = temp; } Program output: 12 4 This time, the modifications to a and b have affected the values of num1 and num2, because they were performed on the values stored in the addresses of num1 and num2.

25 Example: Call by reference #include using std::cout; void freeze (int& temp); int main () { int temperature; freeze(temperature); cout << temperature; return 0; } void freeze (int& temp) { temp = 32; } Program output: 32 This is an example of using call by reference to initialize a value from inside a called function. It is often used when the called function needs to modify several values and pass them back to the caller.