Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Function Overloading: Calling the Same Function Name with Different Parameters, Study notes of Computer Science

Function overloading is a programming technique that allows a program to contain multiple functions with the same name but different parameter types or numbers. The rules for function overloading, its usage, and an example of how to implement it in c++. Overloading functions is useful when the algorithm remains the same, but the expected input varies.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-xe4
koofers-user-xe4 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Function Overloading (pages 908-911)
A program can contain multiple functions with the same name!
EX:
void Print ( int data );
void Print ( string data );
You’ll do this when the algorithm is the same, but the type(s) of the parameters are different.
Rules:
The computer can only distinguish functions with different types or number of arguments . The
number and/or type of parameters expected by the function must be unique for each overloaded
function. The return type is not considered when selecting the appropriate overloaded function.
EX: //this is NOT okay
int Calculate( int data );
float Calculate ( int data );
//this is okay
int Calculate (int data);
int Calculate (int data1, int data2);
int Calculate (float data);
Using in a program
Overloading functions is appropriate when all of the functions use the same algorithm, but should be
called in different scenarios based on their expected input.
int main( ) {
void DebugPrint (int data); //function prototypes
void DebugPrint (string data);
void DebugPrint (float data);
int value; //variable declarations
float average;
string name;
//program statements to assign values to value, average, name, letter and isOk
DebugPrint(value);
Debug Print(average);
DebugPrint(name);
//rest of the program
return 0;
}
void DebugPrint(int data) {
cout << “Debugging “ << data << endl;
}
void DebugPrint(float data) {
cout << “Debugging “ << data << endl;
}
void DebugPrint(string data) {
cout << “Debugging “ << data << endl;
}

Partial preview of the text

Download Function Overloading: Calling the Same Function Name with Different Parameters and more Study notes Computer Science in PDF only on Docsity!

Function Overloading (pages 908-911) A program can contain multiple functions with the same name! EX: void Print ( int data ); void Print ( string data ); You’ll do this when the algorithm is the same, but the type(s) of the parameters are different. Rules: The computer can only distinguish functions with different types or number of arguments. The number and/or type of parameters expected by the function must be unique for each overloaded function. The return type is not considered when selecting the appropriate overloaded function. EX: //this is NOT okay int Calculate( int data ); float Calculate ( int data ); //this is okay int Calculate (int data); int Calculate (int data1, int data2); int Calculate (float data); Using in a program Overloading functions is appropriate when all of the functions use the same algorithm, but should be called in different scenarios based on their expected input. int main( ) { void DebugPrint (int data); //function prototypes void DebugPrint (string data); void DebugPrint (float data); int value; //variable declarations float average; string name; … //program statements to assign values to value, average, name, letter and isOk DebugPrint(value); Debug Print(average); DebugPrint(name); … //rest of the program return 0; } void DebugPrint(int data) { cout << “Debugging “ << data << endl; } void DebugPrint(float data) { cout << “Debugging “ << data << endl; } void DebugPrint(string data) { cout << “Debugging “ << data << endl; }