1 year ago

#383374

test-img

chengi666

Precedence of function call in C++ and Python

I have two similar codes in C++ and Python.
C++ Code

#include <iostream>
using namespace std;

int i = 0;

int fcn()
{
    i = 1;
    return 1;
}

int main()
{
    i = 0;
    i = i + fcn();
    cout << i << endl;
}

Output: 2

The output is 2, and I think it is because of the precedence of function call is higher than addition.

Python code

def fcn():
    global x
    x = 1
    return 1

x = 0
x = x + fcn()
print(x)

Output: 1

The output is 1, which I am not sure how it works.
This page says that function call also has higher priority than addition in Python.
So, why does these two codes have different ouputs, is there any misunderstanding?

python

c++

operator-precedence

0 Answers

Your Answer

Accepted video resources