Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can we use function on left side of an expression in C and C++?
In C, you cannot use a function call on the left side of an assignment if it returns a value because function calls return non-assignable values. For example,
function_name() = value;
However, if the function returns a pointer, you can dereference it to assign a value. For example,
*function_name() = value;
In C++, the same rule applies. You can use the returned reference/pointer on the left side to modify the original variable which is valid only if it returns a reference.
function_name() = value;
Note: References allow direct access to variables, and pointers allow indirect access via dereferencing.
Understating Use of Function on Left Side of an Expression
Consider the following statement:
int x = getValue(); // getValue() is on the right side
If you place something on the left side of an assignment (e.g: x = 10;), that must be a modifiable in memory location which is called an lvalue.
getValue() = 10; // Is this allowed?
If you are trying to assign a value to whatever the function returns. This will happen when a function returns a reference in C++ or a pointer dereference in C.
getReference() = 10;
When returning a reference, be careful that the object being referred to does not go out of scope. So it is not legal to return a reference to local var. But you can always return a reference on a static variable.
int& func() {
int q;
//! return q; // Compile time error
static int x;
return x; // Safe, x lives outside this scope
}
Let us try to understand this concept in the following examples.
Example 1: Using Function on Left Side of Expression in C++
This program shows how a function can return a reference to a variable by changing that variable through the function call.
#include<iostream>
using namespace std;
// Function returns a reference to a static variable
int& getRef() {
static int x = 5;
return x;
}
int main() {
cout<<"Before: "<<getRef()<<endl;
// Assigning through function returning reference
getRef() = 100;
cout<<"After: "<<getRef()<<endl;
return 0;
}
Following is the output to the above program:
Before: 5 After: 100
Example 2: Using Function on Left Side of Expression in C
This program uses a function that returns a pointer to a variable and change the variable's value by dereferencing pointer.
#include<stdio.h>
// Function returns a pointer to a static variable
int* getPtr() {
static int x = 5;
return &x;
}
int main() {
printf("Before: %d\n", *getPtr());
// Assigning through pointer dereference
*getPtr() = 100;
printf("After: %d\n", *getPtr());
return 0;
}
Following is the output to the above program:
Before: 5 After: 100
Example 3: Using Function on Left Side of Expression in C/C++ (Invalid Case)
This program assigns a value to the result of a function that returns a copy (not a reference). So, you can't modify a temporary value which can causes a compiler error.
#include <stdio.h>
// Function returns a value (not a pointer)
int getValue() {
return 5;
}
int main() {
// This will cause a compiler error
getValue() = 10; // Error: lvalue required as left operand of assignment
return 0;
}
Following is the output to the above program:
main.c: In function 'main':
main.c:8:15: error: lvalue required as left operand of assignment
8 | getValue() = 10; // Error: lvalue required as left operand of assignment
|
#include<iostream>
using namespace std;
// Function returns a value (not a reference)
int getValue() {
return 5;
}
int main() {
// This will cause a compiler error
getValue() = 10; // Error: expression is not assignable
return 0;
}
Following is the output to the above program:
main.cpp: In function 'int main()':
main.cpp:9:12: error: lvalue required as left operand of assignment
9 | getValue() = 10; // Error: expression is not assignable
| ~~~~~~~~^~