Home > Blockchain >  Assign Value to function,why we are not able to assign the value to function when it return the valu
Assign Value to function,why we are not able to assign the value to function when it return the valu

Time:07-27

#include<stdio.h>

int function  (){

    int a  = 10; 
    return a; //Here we are return the value 
}

int main()
{

function() = 3;//And here we assign the value 3 to return value then why it causes error 
               

}

CodePudding user response:

The call to function() evaluates to 10, so the assignment in your main() function evaluates to:

10 = 3;

10 is not an lvalue, hence you cannot assign to it.

CodePudding user response:

In C, it is not avaliable to have function names on the left side of an expression.

But you can try it by using C , it will work!

#include <iostream>
using namespace std;

int &fun() {
  static int x;
  return x;
}

int main() {
  fun() = 10;

  // this line will print 10
  printf(" %d \n", fun());

  return 0;
}
  • Related