in Programming in C edited by
619 views
0 votes
0 votes

Can we assign a value to a variable in calling a function as function argument?

Ex-
 

Int Func c( int);
Int i =3;

Void main(){
Fun c ( i=3)}
Int Func c (int x)
{
Int x++;
return x;
}


Please pardon me as the code above is not accurate but can give an idea of what i am trying to ask.

in Programming in C edited by
619 views

13 Comments

Yes,we can.

#include <iostream>
using namespace std;
int func(int x)
{
    x++;
    return x;
}
int main() {
    int x;
    int y=func(x=3);
    cout<<y;
    return 0;
}


Output:4
0
0

@srestha I have tested this in codeblocks and in geeks ide.

I don't know if there is some rule or something which contradicts it.

0
0

@shreys02

I havenot seen it before.

Assignment of a value in a variable inside a function call.

Why we donot do it every time?? Yes possible for a single variable.

But not very sure, why we not use everywhere?

0
0
Yes it is possible. The code will work fine. Here to understand how it works ,we have use the concept of sequence point.
1
1
yes i=3 will give sequence point behavior. Because variable called but not terminated. right??
0
0

According to the concept of sequence points, there is a sequence point before a function is entered in a function call. 

And sequence point says all the side effects from previous evaluations will be performed.

Here i=3 as a argument in the function does not create a sequence point. Any expression, if present as an argument to the function, then its side effect must be evaluated before entering the function. For example , if the statement was func( i++ ) , then there is also a sequence point. Even in the statement func(i) , there is also a sequence point.

Hence in the given code, in the statement 

func(i=3) , there is a sequence point before entering into the function. Thus all side effects will be evaluated by this point. Here the statement i=3 has a side effect of assigning 3 to i , in addition to returning the right operand i.e. 3. Hence i will get the value 3 and the statement becomes func(3). 

0
0

@SuvasishDutta

check this code

Int Func c (int x)
{
Int x++;
return x;
}

Here $x$ is initialized $2$ times, which gives error in C code. Moreover when $c(i=3)$ function calling happens, initialization of $x$ and post-increment $x$, before a semicolon(initialization and increment of $x$ without a semicolon, violating sequence-point), RIGHT?

So, it should give an error. Isnot it?

0
0

@srestha mam, in your code there will be a compiler error in the line 

int x++; here is an error. So the code will not work.

If the code goes like as shown below:

int func(int x)
{
    x++;
    return x;

Here 4 will be returned to the main().

Or if the code goes like as shown below:

int func(int x)
{
    int x;
    return x;
}

Here some garbage value will be returned to the main(). 

Error occurs if within a same scope , a particular variable is declared more than once. Here within the pair of braces in function , which is a scope, x is declared only once. Please dont get confuse with x as a parameter and x as a local variable in the function. The variable x as a function parameter is declared outside the pair of braces i.e. outside the scope where local variable x is declared. 

When a function is invoked or called, before entering into the scope of function i.e. before execution of the first instruction of the function starts, the parameter x is declared. Then the control goes into the scope of the function.

In C, same type of variables but different scope or visibility can be declared withot any error.

 

1
1

Here within the pair of braces in function , which is a scope, x is declared only once. Please dont get confuse with x as a parameter and x as a local variable in the function.

That means, u r telling, though  x declared twice, they are not in same scope. So, no error in previous code. Right??

But what about sequence point error if x??

0
0
There will be no undefined behaviour in every examples shown above.

Undefined behaviour in a program occurs if there are multiple updates between two sequence points i.e. between previous and next sequence points.
0
0

@SuvasishDutta

It is assigning the value of x=3, and then post-incrementing , Is it not two operation before a semicolon?

0
0

@srestha mam, please kindly post the code of your question. I am not getting the code based on your question.

0
0

no no, it is not my question. This line I written in last line, is for this question 

See these line of the question and check my statement

Fun c ( i=3)}
Int Func c (int x)
{
Int x++;
return x;
}

 

0
0

Please log in or register to answer this question.

Related questions