-3

This code is not working. Did you guys have any method to

Pass argument from a Function Definition to another Function Definition without returning back to main functions ?

If possible try not to move the main function to end.

#include <stdio.h>

int test();
int new(int);

int main()
{
    test();

    int num;
    int new; 
    printf("The num is : %d\n", num);
    printf("The new num is : %d\n",new);

 }

int test()
{
     int num;

     printf("Enter a number: ");
     scanf("%d",&num);
     new (num);
     return num;

 }

 int new(int num)

{
    int new;
    new = num*2;
    return new;
}
Jeremy
  • 55
  • 6
  • You say the code is not working, but do not explain what it does do, or what you expect it to do. It looks like nonsense, so it would be better to explain what you are trying to do. Yes you can pass an argument to a function from another function - what makes you think that you can't? You return a value from `new()` but discard it in `test()`, the _variables_ `new` and `num` in `main()` are unitialised. Be aware that these variables are not related to the variables of the same name in `test()` and `new()`. – Clifford Sep 08 '18 at 19:07

2 Answers2

0

Though question is not clear & there are lot of unused variable like

int main(void) {
    test();

    int num; /* what's the purpose of this after calling test() */
    int new; 
    printf("The num is : %d\n", num); /* prints garbage, make sure where u want to use it */
    printf("The new num is : %d\n",new);
    return 0;
 }

Pass argument from a Function Definition to another Function Definition without returning back to main functions ? Use this

return new (num); /* 1st new function gets called, then returned value from test() returned to main() */

instead of

new (num);
return num; /* here num which is declared in test function get's returned */
Achal
  • 11,821
  • 2
  • 15
  • 37
  • Maybe you don't really understand my question. I still can't change the result after I changed it to `return new (num)`. But still thanks. – Jeremy Sep 08 '18 at 17:06
  • If you want to change the value of `num` without returning it then you should pass the address of num. This https://stackoverflow.com/questions/29088971/parameter-passing-in-c-pointers-addresses-aliases will surely help you. – Achal Sep 08 '18 at 17:19
  • Thanks man. Even thought the code you wrote I still don't understand or you don't understand mine. But it's okay, the link you comment above here did helped me. The things that I want is "Call-by-address". Thank you for your help :) – Jeremy Sep 08 '18 at 17:45
  • @Jeremy -- note that C is call-by-value _always_. [There is an answer, but not the accepted answer](https://stackoverflow.com/a/29089547/6879826), on the linked question that gets this right. Passing a pointer is still call-by-value in C, because a copy of the pointer value is used in the function. But, that (copied) pointer value allows the function access to the value which the pointer references. – ad absurdum Sep 08 '18 at 17:52
  • @Jeremy : as Achal states at the start your "_question is not clear_"; if he has failed to understand your question, the fault is with you, not him. Read the comments against your question and fix the question, and you will get a better answer. – Clifford Sep 08 '18 at 19:10
0

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.

ad absurdum
  • 19,498
  • 5
  • 37
  • 60