Just like you can assign a value literal in variable , you can assign a expression since you made a subtraction, which is an expression. An expression can contain a number of things that generate some results. A function generates a result, so just use it in the assignment of the variable. See term explanations .
I took advantage of and simplified the code.
#include <stdio.h>
int distanciaCorrida(int inicio, int fim) {
return fim - inicio;
}
int main() {
int kminicial = 200000;
int kmfinal = 207349;
int distancia = distanciaCorrida(kminicial, kmfinal);
printf("A distancia percorrida pelo carro foi de %d km", distancia);
}
See running on ideone . And on the Coding Ground (you're in trouble, post it). Also put it on GitHub for future reference .
The variable distancia
is not even there, in fact all of them, but to do it in the same way and indicate how to do it if it were something that the variable is needed. Could do:
printf("A distancia percorrida pelo carro foi de %d km", distanciaCorrida(kminicial, kmfinal));
Except in a case of wanting to generate an abstraction, it is even simpler to do the simple straightforward calculation without creating a function, but I understand it to be a learning issue. You just can not learn to do it like this if you do not have to.
A good looking question .