why the output is 5 and 2 & not 25 and 4

+1 vote
asked Jun 28, 2018 by anonymous
#include <stdio.h>
void fun( int, int);
int main()
{
    int i=5,j=2;
    fun(i,j);
    printf("%d %d\n",i,j);
    return 0;
}    

void fun(int i, int j)
{
    i = i*i;
    j = j*j;
}

7 Answers

+3 votes
answered Jun 28, 2018 by Sai Naresh Dasari
The function hasn't returned any value back to the main function.hence the main the values of the i,j declared in the initial function are displayed.
commented Jun 30, 2018 by anonymous
Its call by value so the value is not updated.
0 votes
answered Jul 5, 2018 by arun krishnan
#include <stdio.h>
void fun( int *p, int *q);
int main()
{
    int i=5,j=2;
    fun(&i,&j);
    printf("%d %d\n",i,j);
    return 0;
}    

void fun(int *p, int *q)
{
    
    *p = (*p)*(*p);
     *q = (*q)*(*q);
}
0 votes
answered Jul 5, 2018 by Dennis munyu (140 points)

I think the reason is that your function is returning nothing or has nothing to return try this ;

#include <stdio.h>
void fun( int, int);
int main()
{
    int i=5,j=2;
    fun(i,j);
 
    return 0;
}    

void fun(int i, int j)
{
    i = i*i;
    j = j*j;

   printf("%d %j",i,j);
}

commented Jul 6, 2018 by anonymous
void fun(int i, int j)
{
    i = i*i;                                      // change this to:        i *=i;   or i**;
    j = j*j;                                      // change this to:         j *=j;  or j**;

   printf("%d %j",i,j);              // change this to:        printf("%d %d", i, j);
}

Or:

int fun(int i, int j){
        i = i*i;      // this is not wrong, but the shorter the code the less time it takes to process.
        j = j*j;      // though, not always better.
return 0;
}
0 votes
answered Aug 9, 2018 by daiso

#include <stdio.h>
void fun( int *p, int *q);
int main()
{
    int i=5,j=2;
    fun(&i,&j);
    printf("%d %d\n",i,j);
    return 0;
}    

void fun(int *p, int *q)
{
    
    *p = (*p)*(*p);
     *q = (*q)*(*q);
}

벳365가상축구​

0 votes
answered Aug 9, 2018 by skater
Because the value taken in the parameters of function fun are not by refrence. So the values wont change when they come back to main for the execution. hence there is no change in the value of i and j in the main and the original value is printed.
0 votes
answered Jan 25, 2019 by Jyothi_Rk
Hi,

In main() you are calling the fun() by value ,hence value is duplicated and multiplication is performed on the copied variables(not the original).

If you want the action to be performed on the original variables,then you have to call the fun() by address (i.e,by using pointer).
0 votes
answered Jan 26, 2019 by Ankit suwalka
#include <stdio.h>
void fun( int*i, int*j);
int main()
{
    int i=5,j=2;
    fun(&i,&j);
    printf("%d %d\n",i,j);
    return 0;
}    

void fun(int * i, int* j)
{
   * i = (*i)*(*i);
    *j = (*j)**j;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...