I need to use the return value of function1 as parameter in function2. I've seen other community posts about this, but they didn't help much. When I call function1 inside function2, it's as if I hadn't called function1 as a parameter to function2.
#include<stdio.h>
int n1=0;
int p1=0;
int p2=0;
int dobrar(p1){
return (2*n1);
}
int triplica(p2){
return (3*n1);
}
int main(){
printf("digite um numero \n");
scanf("%i",&n1);
printf("o n1 dobrado %i \n ", dobrar(n1));
printf("o numero triplicado %i",triplica(dobrar(n1)));
return 0;
}
If I put n1=1
the output is: printf = 2 and printf=3. I thought it would be the output printf=2 and printf=6.
CodePudding user response:
As noted in the comments, you want to shy away from using global variables. And, for sure, you really should not utilize global variable within your function definition.
Reviewing the spirit of your project following are two methods for coding that shy away from global variables and provide a more usual and customary way to utilize functions; whereby, you can call functions within functions.
The first code snippet has the helper functions completely defined prior to them being called within the main function.
#include <stdio.h>
#include <stdlib.h>
int dobrar(int n2)
{
return (2*n2);
}
int triplica(int n2)
{
return (3*n2);
}
int main()
{
int n1=0;
printf("digite um numero: ");
scanf("%i",&n1);
printf("o n1 dobrado %i \n", dobrar(n1));
printf("o numero triplicado %i\n",triplica(dobrar(n1)));
return 0;
}
The second way is to provide function prototypes to let the compiler know the signature of the function prior to its actual definition.
#include <stdio.h>
#include <stdlib.h>
/* Prototypes of your functions */
int dobrar(int);
int triplica(int);
int main()
{
int n1=0;
printf("digite um numero: ");
scanf("%i",&n1);
printf("o n1 dobrado %i \n", dobrar(n1));
printf("o numero triplicado %i\n",triplica(dobrar(n1)));
return 0;
}
int dobrar(int n2)
{
return (2*n2);
}
int triplica(int n2)
{
return (3*n2);
}
Also, note that the global variables are not needed anymore. Running this program produces the results you apparently are after where the doubling function is called within the tripling function, producing a net value of multiplying the original entry by six.
Give that a try.
CodePudding user response:
When I call function1 inside function2, it's as if I hadn't called function1 as a parameter to function2
I'm not sure what you mean by that, because I don't see any function calls inside your dobrar
and triplica
functions, which I assume are the functions you are talking about.
However, I suspect that what you are observing is that your particular triplica()
function's return value does not depend on the argument with which it is called. That's among its general properties, not specific to cases where one of the arguments is the return value of another function call. Consider the implementation:
int triplica(p2){ return (3*n1); }
Within the function body, the argument cam be accessed via name p2
, but that doesn't appear outside the parameter list. You can test that:
n1 = 1;
printf("%d\n", triplica(2));
printf("%d\n", triplica(3));
printf("%d\n", triplica(4));
(Prints "3" three times.)
If the objective is to return triple the value of the argument, then that would look like this:
int triplica(int x) {
return 3 * x;
}
Note well the appearance of the parameter name (x
in this case) in the expression for the return value.
Similar applies to your dobrar()
function (test it, too), but the way you use it in the sample program happens to result in the behavior you expected anyway.
In many cases, the compiler would notice such an error and reject the code, but in your case you have a global variable named n1
, which is what the compiler took your function to be referring to. Reasonable uses for global variables occasionally arise, but you should not create a global variable unless you can explain clearly why it needs to be global.
And be sure to give your variables meaningful names. This will help you avoid using the wrong variable for a given purpose, and it will reduce the likelihood of confusion arising from using the same or similar variable names for unrelated purposes.