// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.

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

1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
1 6/20/2015CS150 Introduction to Computer Science 1 Functions Chapter 6, 8.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
#include using namespace std; void main() { cout
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
1 11/8/06CS150 Introduction to Computer Science 1 More Functions! page 343 November 8, 2006.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Functions Parameters & Variable Scope Chapter 6. 2 Overview  Using Function Arguments and Parameters  Differences between Value Parameters and Reference.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
#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 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Chapter 6 Functions. Topics Basics Basics Simplest functions Simplest functions Functions receiving data from a caller Functions receiving data from a.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - 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.
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.
CS1201: Programming Language 2 Function I By: Nouf Aljaffan Edited by : Nouf Almunyif.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
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 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Introduction to Programming
Functions.
Dr. Shady Yehia Elmashad
Chapter 3 - Functions Outline 3.1 Introduction
Chapter 5 Classes.
FUNCTIONS IN C++.
Dr. Shady Yehia Elmashad
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Functions Najah Alsubaie Kingdom of Saudi Arabia
Dr. Shady Yehia Elmashad
Extra.
Functions.
CS150 Introduction to Computer Science 1
Dr. Khizar Hayat Associate Prof. of Computer Science
Variables have attributes
Chapter 15 - C++ As A "Better C"
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Functions Imran Rashid CTO at ManiWeber Technologies.
Dr. Khizar Hayat Associate Prof. of Computer Science
CS1201: Programming Language 2
Programming Fundamental
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return 0; } void function1() { cout << "function1 takes no arguments" << endl; } void function2( void ) { cout << "function2 also takes no arguments" << endl; }

Example #include using namespace std; void printStars(); int main() { printStars(); //Line 1 cout << "********** Annual ***********" << endl; //Line 2 printStars(); //Line 3 cout << "******* Spring Sale **********" << endl; //Line 4 printStars(); //Line 5 return 0; } void printStars() { cout << "******************************" << endl; }

solution #include using namespace std; void square( int m ); // function prototype //it can be int square( int ); int main() { for ( int x = 1; x <= 10; x++ ) square( x ) ; //calling statement x is actual parameter cout << endl; return 0; } // Function definition void square( int y ) // Heading y is Formal Parameter { return y * y; // The return Statement }

#include using namespace std; void square( int m ); // function prototype //it can be int square( int ); int main() { for ( int x = 1; x <= 10; x++ ) square( x ) ; //calling statement x is actual parameter cout << endl; return 0; } // Function definition void square( int y ) // Heading y is Formal Parameter { cout<< y * y<<endl; // The return Statement }

) Reference and Value parameters)

Modify (Value parameters) #include using namespace std; void modify( int m ); // function prototype int main() { int x; x=7; cout<<"x befor calling modify function = "<<x<<endl; modify( x ) ; //calling statement x is actual parameter cout<<"x After calling modify function = "<<x<<endl; cout << endl; return 0; } // Function definition void modify( int y ) // Heading y is Formal Parameter { y=y+10; // The return Statement cout<<"y inside modify function = "<<y<<endl; }

Reference parameters #include using namespace std; void modify( int &m ); // function prototype int main() { int x; x=7; cout<<"x befor calling modify function = "<<x<<endl; modify( x ) ; //calling statement x is actual parameter cout<<"x After calling modify function = "<<x<<endl; cout << endl; return 0; } // Function definition void modify( int &y ) // Heading y is Formal Parameter { y=y+10; // The return Statement cout<<"y inside modify function = "<<y<<endl; }

Reference // References must be initialized #include using namespace std; int main() { int x = 3, &y = x; // y is now an alias for x cout << "x = " << x << endl << "y = " << y << endl; y = 7; cout << "x = " << x << endl << "y = " << y << endl; return 0; }

// A scoping example #include using namespace std; void a( ); // function prototype void b( ); // function prototype void c( void ); // function prototype int x = 1; // global variable int main() { int x = 5; // local variable to main cout << "local x in outer scope of main is " << x << endl; { // start new scope int x = 7; cout << "local x in inner scope of main is " << x << endl; } // end new scope cout << "globel x in outer scope of main is " << ::x << endl; cout << "local x in outer scope of main is " << x << endl; cout<<"*************************************************"<<endl; a(); // a has automatic local x b(); // b has static local x c(); // c uses global x cout<<"*************************************************"<<endl; cout<<"Afetr second calling"<<endl; a(); // a reinitializes automatic local x b(); // static local x retains its previous value c(); // global x also retains its value cout <<endl<< "local x in main is " << x <<endl<< endl; return 0; }

void a( ) { int x = 25; // initialized each time a is called cout << endl << "local x in a is " << x << " after entering a" << endl; } void b( ) { static int x = 50; // Static initialization only // first time b is called. cout << endl << "local static x is " << x << " on entering b" << endl; x=x+2; } void c( void ) { cout << endl << "global x is " << x << " on entering c" << endl; x *= 10; } in first call 10 after second call 1 25 in secon d call 52 in Second call call

// Using overloaded functions #include using namespace std; int square( int x ); double square( double y ); int main() { cout << "The square of integer 7 is " << square( 7 )<<endl << "The square of double 7.5 is " << square( 7.5 ) << endl; return 0; } int square( int x ) { return x * x; } double square( double y ) { return y * y; }

// Using default arguments #include using namespace std; int boxVolume( int length = 1, int width = 1, int height = 1 ); int main() { cout << "The default box volume is: " << boxVolume() <<endl; cout<< "width 1 and height 1 is: " << boxVolume( 10 ) << endl; cout << "width 5 and height 1 is: " << boxVolume( 10, 5 ) << endl; cout << "width 5 and height 2 is: " << boxVolume( 10, 5, 2 ) << endl; return 0; } // Calculate the volume of a box int boxVolume( int length, int width, int height ) { return length * width * height; }