Parameter, Scope & Overloading

Parameter, Scope & Overloading - CHAPTER 3 & 4...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
PARAMETER, SCOPE AND OVERLOADING
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
In this chapter, you will learn about: void functions Difference between value and reference parameters Explore reference parameters and value- returning functions Scope of an identifier Difference between local and global identifiers static variables Function overloading Explore functions with default parameters
Background image of page 2
Void Functions without Parameters Syntax Function Definition void functionName( void ) { statements } The word void inside the parentheses is optional. In C++, void is a reserved word. Syntax Function Call functionName();
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
#include <iostream> using namespace std; void printStars(void); 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( void ) { cout<<"******************************"<<endl; cout<<"******************************"<<endl; }
Background image of page 4
VOID FUNCTIONS In a void function, you can use the return statement without any value; it is typically used to exit the function early. Void functions may or may not have formal parameters.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Void Functions with Parameters Syntax Function Definition void functionName(formal parameter list) { statements } Syntax Formal parameter list : dataType variable , dataType variable, . ..
Background image of page 6
Syntax Function Call functionName(actual parameter list); Syntax Actual Parameter List expression or variable , expression or variable, …
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: void funexp( int a, double b, char c, int { . . . }
Background image of page 8
Value Parameter: A formal parameter that receives a copy of the content of the corresponding actual parameter. Reference Parameter: A formal parameter that receives the location (memory address) of the corresponding actual parameter. When we attach after the dataType in the formal parameter list of a function, then the variable following that dataType becomes a reference parameter. Example: void expfun( int one, int & two, char three, double & four);
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
If a formal parameter is a value parameter, the value of the corresponding actual parameter is copied into the formal parameter. The value parameter has its own copy of the data. During program execution, the value parameter manipulates the data stored in its own memory space.
Background image of page 10
If a formal parameter is a reference parameter, it receives the address of the corresponding actual parameter. A reference parameter stores the address of the corresponding actual parameter. During program execution to manipulate the data, the address stored in the reference parameter directs it to the memory space of the corresponding actual parameter.
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Example: Given the course score (a value between 0 and 100 ), the following program determines the course grade of a student. 1.
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2008 for the course CS 115 taught by Professor Ryon during the Fall '06 term at NJIT.

Page1 / 82

Parameter, Scope & Overloading - CHAPTER 3 & 4...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online