Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Programming Languages and Paradigms The C Programming Language.
F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
C++ Pointer and Functions
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
Classes: A Deeper Look Systems Programming.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
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.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Pointer Data Type and Pointer Variables
C++ for Engineers and Scientists Third Edition
1 Chapter 9 Scope, Lifetime, and More on Functions.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Learners Support Publications Classes and Objects.
Fundamentals of C and C++ Programming. EEL 3801 – Lotzi Bölöni Sub-Topics  Basic Program Structure  Variables - Types and Declarations  Basic Program.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
C++ Class Members Class Definition – class Name – { – public: » constructor(s) » destructor » function members » data members – protected: » function members.
Liang, Introduction to C++ Programming, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Advanced Function Features.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
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.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 6 Functions.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Structs and Classes Structs A struct can be used to define a data structure type as follows: struct Complex { double real, imag;} // specifying a Complex.
Engineering Classes. Objectives At the conclusion of this lesson, students should be able to: Explain why it is important to correctly manage dynamically.
Review of Function Overloading Allows different functions to have the same name if they have different types or numbers of arguments, e.g. int sqr(int.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
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.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
1 CSC 533: Programming Languages Spring 2014 Subprogram implementation  subprograms (procedures/functions/subroutines)  subprogram linkage  parameter.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Java Programming Language Lecture27- An Introduction.
C Part 2 Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens The Three Attributes of an Identifier Identifiers have three essential.
Memory Management.
Functions.
Chapter 7: User-Defined Functions II
The Three Attributes of an Identifier
Programmazione I a.a. 2017/2018.
User-Defined Functions
C++ for Engineers and Scientists Second Edition
Scope, Parameter Passing, Storage Specifiers
Classes and Objects.
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Corresponds with Chapter 5
Presentation transcript:

Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type, type, …); // Strong type checking The return_type: A function that does not return a value should declare a return_type of void. A return statement is not necessary in a function with a void return type (however, it can still be used to terminate the function), void Fun(){ if(cond) return; ….} The default return_type is int (e.g., in function main(), return 0; is found) Neither an array or a function can be specified as a return_type, int[10] fun(); // Error. The Argument_list: Functions are allocated storage on a structure called the program’s run-time stack, this stack is automatically popped when the function terminates. Arguments are passed by-value (the entire object is copied), or by-reference (only the address of the argument is copied) Arrays are passed by reference using a pointer to the zeroth element of the array Arguments can have default values, and functions can be overloaded

Functions, Scope, and The Free Store Default Values for the Argument_list A function may specify default values for all or only a subset of its arguments, int fun(int i= 0);// a function prototype with a default argument initializer int fun(int i = 0){//fun body….} // Error, a default value must be specified only once in a file, by convention it should be in the declaration The right most argument must have a default value before any argument to its left, fun(int i= 0, double x); // Error, default order is incorrect fun1(double x, int i= 0, char* str); // Error, the right most is not initialized fun1(double x, int i=0; char* str= “No Free Lunch”); // OK the invocation fun1(0.05,, “Hello”); // gives syntax error Function Overloading Several functions can be given the same name provided that each have a unique Argument_list, e.g. the following functions can exist in the same file, int max(int,int); // returns the max of two integers int max(const int *, int); // the first argument is a pointer int max(const List &); // returns the max of a List type object Avoid Ambiguous overloading: max(const int, int); // ambiguous with max(int,int) print(unsigned int); print (int); // suppose we have the invocation print(100);

Functions, Scope, and The Free Store Identifier Scoping and Storage Classes The scope of identifiers has three types, 1. File Scope: identifier is declared in the file and not within a class or within a function 2. Local scope: identifiers are declared within a function 3. Class scope: identifiers are declared within a class For example, enum Boolean {false,true}; // Boolean has a file scope class X{ // X has a file scope Boolean fun(); // fun has a class scope } const int Max = 100; // Max has a file scope X x_obj; // x_obj has a file scope main(){ Boolean Quit; // Quit has a local scope int fun();// fun has local scope} Boolean X::fun(){…} int fun(){…..}// now fun has the file scope // the above is not function overloading

Functions, Scope, and The Free Store File Scope The outer most scope which encloses both local scope and class scope The scope extends from the point of declaration in the file to the end of the file Storage classes static and extern are used to specify whether the same identifier can be declared and used in other files, e.g., in file x.cpp: extern const int Max = 100; // Max can be used in other files static int Max_size=1000; // Max_size can not be used in other // files, i.e., it does not have external linkage in file y.cpp: extern const int Max; // this does not allocate storage for Max // Max has already been defined in x.cpp extern int Max_size; // Error Max_size has static file scope Constants by default are static(i.e., no external linkage), while variables by default are extern. Class names can be used in other files but there are conditions or rules for that, it is better to put the class declarations in a header file and include this file in different files as shown in the lab. assignment examples typedef, enum, and template names can not have external linkage

Functions, Scope, and The Free Store Local Scope consists of function scope and block scope function scope: extern const int Max; // global constant max defined and initialized in another file funny(){ int I = 100; // local variable const int Max = :: Max * I; // the scoping operator is used to //access the global Max to initialize the local Max } Block Scope: while(cond){ int I = 0; I = 500; // this I is local to the while block if (cond) {int I = 0; // this is a different I local to the if block} } Static local variables {static int I=0; // I retains its most recent values and stays alive until the // program terminates I++; } // I will contain the number of times this block is executed

Functions, Scope, and The Free Store The following code will not produce a syntax error but will produce a dangling pointer, a pointer that points to no object, int * sqr(int x) { int y; y = x*x; return &y; }// the data object y does not exist after the termination of this function main(){ int * p = sqr(100); // p is a dangling pointer // and is initialized to point to an object which has been destroyed } // the above will different if y is declared as a static storage class using static int y; // y will still exist even after sqr() terminates