Hello

Hi, I am new in Programming and I wrote this simple C program, This program get executed but I was wondering why I cannot get 70 in output. Thank you in advance.

#include<stdio.h>
void fun(int);
void replace(int);
int main()
{
int a = 30;
fun(a);
printf("%d\n", a);
return 0;

}
void fun(int b)
{

b = 60;

printf("%d\n",b);
return b;

}
void replace(int c )
{

c= 70;
printf("%d\n",c);

}
first of all you are returning a int in your function "void fun(int b)" which returns nothing (void), if you want it to return int replace void with int.

and be more precise, why do you expect the output to be 70?

Thanks leryss, I just wanted to see how a function works, I got 30 and 60 but not 70 so I was wondering why? I did the following replacement but still did not get 70. I tried by replacing all void by int and still it doesn't work.



#include<stdio.h>
void fun(int);
int replace(int);
int main()
{
int a = 30;
fun(a);
printf("%d\n", a);
return 0;

}
void fun(int b)
{

b = 60;

printf("%d\n", b,b);
return b;

}
int replace(int c )
{

c= 70;
printf("%d\n",c);

}
Last edited on
in your int main() you called fun() function instead of replace(which returns no value), I was talking about the fun() function that should return int. first you have to call the function if you want to use it, like you called fun() in int main

functions don't call themselves that would be a disaster since libraries could contain thousands of them
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<stdio.h>

int fun(int); // <--
int replace(int);

int main()
{
   int a = 30;
   fun(a);
   printf("%d\n", a);
   
   replace(1234); // <--
   
   return 0;
}

int fun(int b) // <--
{
    b = 60;
    printf("%d\n", b);
    return b; // <--
}

int replace(int c )
{ 
    c = 70;
    printf("%d\n",c);
}
Thanks Leyrss and Kemort. Kemort I run the script you gave me and it works fine. I tried with replace() instead of replace{1234), but that doesn't work. To try myself I used some other random four number , and that works. what is the reason behind this? I noticed even a single digit or a character also works. Thank you so much again.
Last edited on
closed account (48T7M4Gy)
The reason it doesn't work with just replace() is because the function requires an integer.

The function will take any integer you like and you'll notice the printout is always the same. Also if the function had a return c the returned value would be 70. In both cases you can pass any integer to the function which isn't very useful. Also by not having a return doesn't make much sense with the value returned being undefined.

This might make more sense:
int replace(int c )
1
2
3
4
5
{ 
    int d = c;
    printf("%d\n",d);
    return c * 2;
}


and in main()

1
2
int x = replace(123);
std::cout << x;// or use printf(... 


Last edited on
Thanks alot, now is more clear.
Hello,
I am trying to calculate the factorial of an Integer and wrote the following program, I can run this Program but cant get the output, can anyone help me please. Thanks in Advance.
/*I want to calculate the factorial of an Integer*/

#include<stdio.h>
int factorial(int);
int mian()
{
int a, fact;
printf("enter the number\n");
scanf("%d",&a);
fact = factorial(a);
printf("Factorial value = %d\n", fact);
return 0;
}
int factorial(int x)
{
int f = 1, i;
for(i=x;i>=1;i--)
f = f*i; /*factorial calculation*/
return (f);

}
closed account (48T7M4Gy)
int main()
Thanks Kemort, got it.
I have compiled this code and it works. I am quiet confused about the output, as it is Boolean it should be 1 and 0, but for b+b it gives 2, can some body explain it briefly please.
Thanks in advance.
#include<iostream>
using namespace std;
int main()
{
bool b =32;
int i = false;
cout<<endl<<b<<endl<<i;
int j = b+b;
bool k = b+b;
cout<<endl<<j<<endl<<k<<endl;
return 9;

}
closed account (48T7M4Gy)
Hemanti123

bool is designed to have only two values, true or false, and to be used with logical operators && || etc. So anything outside that is undefined behaviour and not worth experimenting with. Sorry to say your program is a waste of time and effort. :)

PS bool + bool has no meaning in just the asame way that bool * boll or bool / bool does. What does true * true mean or sqrt(false) or Cos(true)? Who knows? Who cares? :)
Last edited on
Topic archived. No new replies allowed.