Same function name but different purpose – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#4485    siteicon   siteicon  

Same function name but different purpose

Function overloading when function name is same but the objective of function is entirely different.

Learning Objectives

  • Understanding overloading with unrelated purpose of functions.

Source Code

TC++ #4485


Source Code

Run Output


Code Understanding

void action(char c) { for(int i=0;i<5;i++) cout<<c;}
Purpose of this function is to print given input character five times. It doesn’t return any thing.

int action(int n) {return n*n;}
Purpose of this function is entirely different. It returns the square of given integer.

int main() {
Call to function with input as a character. Since it doesn’t return any value so there is nothing on the left hand side of expression. The function call in itself is an expression.

Call to function with input as an integer. Since this returns an integer, so the returned integer can be directly fed to the console stream, hence it is a part of cout chain of action.

return 0;


  • Usually naming a function with same name is done when they have similar purposes. For. eg. in the above example the functions can be named as printfivetimes() and square() respectively for better understanding. The above code has been shown as an example that overloading doesn’t restrict you from naming functions with same name even when purpose is different. This is also a requirement when large teams of programmers is working on a project and you do not want to restrict programmers with very strict naming constraints.

Suggested Filename(s): func-ol3.cpp


sunmitra| Created: 26-Aug-2018 | Updated: 28-Aug-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues