You have a number of problems in this code. Functions that take no arguments require void
in the argument list. int test()
means that this function takes an unspecified number of arguments, while int test(void)
means that this function takes no arguments. Also, scanf()
returns the number of successful assignments made, which could be 0 if the user enters malformed input (or EOF
in the rare event of an input failure). You should always check the values returned by functions that do return values. In this case, failure to do so and act accordingly can lead to undefined behavior if the user enters non-numeric input.
These aren't the cause of your problems, though. First, when a value is returned from a function, the calling code must make use of the value. Code like:
new(num);
return num;
where the new()
function is returning a value does nothing with that returned value.
The other issue is one of variable scope. In the main()
function, for example, num
and new
are not visible in any of the other functions, and similarly the variables num
and new
which belong to the other functions are not visible from main()
. This is a disadvantage of using the same variable names in different functions; it can cause confusion.
In the main()
function, test()
is called, but the return value is not used. Then num
and new
are declared. But these variables are not initialized, and so have indeterminate values. The attempts to print these values leads to undefined behavior.
Here is the first fix:
#include <stdio.h>
int test(void);
int new(int);
int main(void)
{
int new = test();
printf("The new num is : %d\n", new);
}
int test(void)
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
return new(num);
}
int new(int num)
{
int new;
printf("The num is : %d\n", num);
new = num * 2;
return new;
}
Here the value of the original number is printed in the new()
function, and the new value of the number is passed back to test()
by the new()
function, and then back to main()
by the test()
function.
If you want to pass two values back to main()
, you are out of luck. Only one value can be returned from a function in C. But, there are other options.
You could use a pointer, or two pointers, for the interesting values. The pointers would allow functions to modify the values known in main()
without the need to return any value. Remember that C passes function arguments by value, which means that a copy of the value of the original argument is used in the function. This means that any changes that you make to that value are not reflected in the original value. But by passing a pointer to a value, you can change the original value because you have a pointer to the original value (though only a copy of the pointer value):
#include <stdio.h>
void test(int *old_val, int *new_val);
void update(int *old_val, int *new_val);
int main(void)
{
int old_val;
int new_val;
test(&old_val, &new_val);
printf("The num is : %d\n", old_val);
printf("The new num is : %d\n", new_val);
}
void test(int *old_val, int *new_val)
{
printf("Enter a number: ");
scanf("%d", old_val);
update(old_val, new_val);
}
void update(int *old_val, int *new_val)
{
*new_val = *old_val * 2;
}
Another option would be to use a struct
to contain two values, and pass the struct
around (or a pointer to the struct
):
#include <stdio.h>
struct values
{
int old;
int new;
};
struct values test(struct values x);
struct values update(struct values x);
int main(void)
{
struct values my_x;
my_x = test(my_x);
printf("The num is : %d\n", my_x.old);
printf("The new num is : %d\n", my_x.new);
}
struct values test(struct values x)
{
printf("Enter a number: ");
scanf("%d", &x.old);
return update(x);
}
struct values update(struct values x)
{
x.new = x.old * 2;
return x;
}
Another possibility would be to use global variables, but I will not go into that here. Global variables are usually a bad idea that better program design can avoid.