0

I'm learning python currently, earlier I worked on C++. Today I was watching a tutorial on Python concepts, and I saw this code which although works fine on Python but should've given error according to me (or more frankly, according to C++).


Here is the C++ code, which gives error: (Please check out the output below first)

#include<iostream>

int doMath(int a, int b){
    return a+b;
}

int doMath(int c, int d){
    return c*d;
}

int main(){
    std::cout<<doMath(3,4);
    return 0;
}

Output:

In function 'int doMath(int, int)':

error: redefinition of 'int doMath(int, int)' at line 9

note: 'int doMath(int, int)' previously defined at line 5

=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===


And here is the equivalent Python code: (Please check out the output below first)

def doMath(a,b):
    return a+b

def doMath(c,d):
    return c*d

def main():
    print(doMath(3,4))

if __name__ == "__main__": main()

Output:

12

Process finished with exit code 0


So why does python select the second-in-sequence function definition of doMath() on its own? How could it decide by itself that I want the second definition and not first?? Why does it allow multiple definitions under same name and same number of parameters???


Or more precisely, perhaps the question could be, why can't we compile a Python code like we can in C++ (atleast not on PyCharm or few others I know)? Because if we could it would first compile and identify that there are two function definitions under one name with same number of parameters and thus not run at all. Same as good old fashioned C.

2 Answers 2

1

Python is a script-based language, which means that no compilation occurs, simply that code is executed in real time. When you "define" a function in Python, it can be thought of as assigning a function to a name. When you want to call that function, you call the function stored in that name. If you redefine that function, you overwrite that name.

This is different from C++, where code is compiled ahead of time. C++ compiles all functions at once, meaning that a second declaration of doMath would result in ambiguous calls. Python, on the other hand, declares functions as you go. If you reassign doMath, the script will simply call its most recent definition.

To answer your question "Why?": well, that's simply how Python was designed to work. This design has many advantages, such as if you want to change how a function behaves based on the context. Python also has downsides, such as needing an interpretter in order to run (because, like you mentioned, we can't compile Python). If you wanted the same behavior as "good old fashioned C," use C. Different languages have different quirks which may work better in different applications.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd like to use C++ actually over everything, but sadly it's good for nothing language. :(
1

They're very different languages so they behave differently.

Python doesn't have function overloading, so the second definition simply overwrites the first.

C++ does have overloading though, so it tries to treat the second definition as an overload. You can't overload a function with a second definition that has identical parameters as the first though, so it raises an error.

1 Comment

Just came to know that python doesn't support function overloading lol... thanks a lot ^_^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.