Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 4 Modular Programming with Functions.
Advertisements

1 Storage Duration and Scope –Local and global variables Storage classes –automatic, static, external, register Todays Material.
Functions in C++. Functions  Groups a number of program statements into a unit & gives it a name.  Is a complete and independent program.  Divides.
Chapter 7: User-Defined Functions II
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 5 - Functions Outline 5.1Introduction 5.2Program.
Lecture 11 Oct 14, 02. Recursion ► The programs we have discussed are generally structured as functions that call one another in a disciplined, hierarchical.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
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.
Overview of Previous Lesson(s) Over View  C++  KeywordsReserved words  IdentifiersProgrammers defined variables  Variables A named storage location.
Inline Function. 2 Expanded in a line when it is invoked Ie compiler replace the function call with function code To make a function inline the function.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline Function Templates Recursion Example Using Recursion: The Fibonacci Series.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Lecture6 Recursion function © by Pearson Education, Inc. All Rights Reserved. 1.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
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.
Object Oriented Programming Spring COMSATS Institute of Information Technology Functions OOP in C++ by Robert Lafore - Chapter#5 Kaleem Ullah
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.
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Learners Support Publications Functions in C++
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
ECE 103 Engineering Programming Chapter 36 C Storage Classes Herbert G. Mayer, PSU CS Status 8/4/2014 Initial content copied verbatim from ECE 103 material.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
CSCI 171 Presentation 6 Functions and Variable Scope.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
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.
Programming Fundamentals1 Chapter 6 FUNCTIONS AND POINTERS.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
Programming Languages -2 C++ Lecture 3 Method Passing Function Recursion Function Overloading Global and Local variables.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
A First Book of ANSI C Fourth Edition
Functions  Simple Functions  Passing Arguments to Functions  Returning Values from Functions  Reference Arguments  Overloaded Functions  Recursion.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Introduction to C++ Programming Lecture 2 Functions September.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 6 Modularity Using Functions
IS Program Design and Software Tools Introduction to C++ Programming
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Functions and an Introduction to Recursion
A Lecture for the c++ Course
Chapter 5 Functions.
Chapter 5 Conclusion CIS 61.
C-language Lecture By B.S.S.Tejesh, S.Neeraja Asst.Prof.
Chapter 5 - Functions Outline 5.1 Introduction
FUNCTIONS& FUNCTIONS OVERLOADING
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.
6 Chapter Functions.
Variables have attributes
The Function Prototype
Functions and an Introduction to Recursion
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Imran Rashid CTO at ManiWeber Technologies.
Functions Chapter No. 5.
Presentation transcript:

Functions Modules in C++ are called functions and classes. Main reason to use functions is : – get aid in conceptual organization of program. – reduce program size. –avoid repetition of code. A function is invoked by a function call. –Using function name and parameters. Library Functions and User Defined Functions.

#include int square ( ); //function prototype void main( ) { cout << square( ); } int square ( )//function definition { return 5 * 5; } Function Definitions

Passing Arguments to Functions Passing Constants Passing Variables –Passing by Value –Passing by Reference

#include int square (int); //function prototype void main( ) { cout << square( 5); } int square (int y)//function definition { return y * y; } Passing Constants to Function

Function creates new variables to hold the values passed. Initialize these variables with the values passed. Effect on actual variables? Passing Variables - By Value

#include int square (int); //function prototype void main( ) { int x ; cout << “\nEnter a number : ”; cin > x; cout << square(x); } int square (int y)//function definition { return y *y; } Passing Variables - By Value

Rewrite the previous program to get square of numbers from 1 to 10. Write a program using a function max( ) to determine and return the largest of three integers entered by the user. Passing Variables - By Value

Returning Values from Functions The return Statement. Default return type - int. No value returned, if return type is void. If many arguments sent to functions - number of values returned ?

Passing Variables - By Reference A reference to original value is passed. Function has an access to actual variables in calling program. Provides a mechanism to return more than one value from the function.

Passing Variables - By Reference # include void frac (float, float&, float&); void main( ) { float num, intpart, fracpart; cout > num; frac(num, intpart, fracpart); cout<< “\nInteger part is “ <<intpart <<“\nFraction part is “ <<fracpart; }

Passing Variables - By Reference void frac (float n, float& intp, float& fracp) { intp = float( long(n) ); fracp = n - intp; } // ampersand (&) is not used in function call // In function call,no difference in value or // reference parameters.

Overloaded Functions Same –Function name, return type. Different –Number of Arguments or –Type of Arguments

Overloaded Functions #include void square ( ); //function prototype void square (int); void square (int, int); void main( ) { square( ); square (10); square (5,10); }

void square ( ) { cout << endl<<5 * 5; } void square (int y) { cout << endl<<y * y; } void square (int s, int f) {for (; s <=f; s++) cout << endl <<(s * s); } Overloaded Functions

Inline Functions Written like a normal function, but compiles into inline code instead of into a function. Compiler must have seen the function(not just the declaration) before first function call. Definition of inline function before main( ). inline keyword is a request to compiler, which may be ignored.

#include inline float lbstokg ( float pounds) {return * pounds; } void main ( ) {float lbs; cout << “\nEnter weight in pounds : “; cin >> lbs; cout <<“Weight in kilograms is “ << lbstokg(lbs); } Inline Functions

Variables and Storage Classes Lifetime –The time period between the creation and destruction of a variable is called its lifetime or duration. Visibility –A variable’s visibility describes where within a program it can be accessed. Scope –is that part of the program where the variable is visible.

Automatic Variables An automatic variable is not created until the function in which it is defined is called. The word automatic is used as variables are automatically created when a function is called and auto destroyed when it returns. Lifetime of auto var coincides with the time when function is executing. Visible only within the function in which they are defined. No initialization of auto/local variables.

Defined outside of any function. Can be accessed by any function. Exist for life of the program. Visible in the file in which they are defined. External or Global Variables

Defined within a function with keyword static. Visibility of a local variable. Lifetime of an external variable. Initialization takes place only once. Static Variables

# include float getavg(float); void main ( ) { float data=1, avg; while (data != 0) { cout > data; avg = getavg(data); cout << “New average is “<<avg<<endl; } Static Variables

float getavg(float newdata) { static float total=0; static int count = 0; count++; total += newdata; return total / count; } Static Variables

#include unsigned long factorial(unsigned long); void main ( ) { for (int I=0; I <=10; I++) cout <<endl << “Factorial of “ << I << “ = “ <<factorial(I); } Recursive Function - Function calling itself

unsigned long factorial(unsigned long num) { if (num <=1) return 1; else return (num * factorial (num-1)); } Recursive Function - Function calling itself