Reputation:
This is a simple enough problem I'm trying to figure out how to pass the int variables back to the other functions from the input function so it can be used in the stuff function to do the math then it returns the added variable and then passes all three to the output function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int input(int first, int second, int third);
int stuff(int first, int second, int third, int added);
int output(int first, int second, int third, int added);
int main(){
int first,second,third;
int added;
//sub functions
input(first,second,third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
int input(int first, int second, int third){
printf("Enter an interger for #1:");
scanf("%d",&first);
printf("Enter an interger for #2:");
scanf("%d",&second);
printf("Enter and interger for #3:");
scanf("%d",&third);
return first,second,third;
}
int stuff(int first, int second, int third, int added){
added = first + second + third;
return added;
}
int output(int first, int second, int third, int added){
printf("Integer 1 = %d\n",first);
printf("Integer 2 = %d\n",second);
printf("Integer 3 = %d\n",third);
printf("Integer 1,2,3 added together = %d\n",added);
}
Upvotes: 0
Views: 61
Reputation: 96927
You could do the following:
void input(int *a, int *b, int *c);
int main()
{
int first, second, third;
/* ... */
input(&first, &second, &third);
/* ... */
}
void input(int *a, int *b, int *c)
{
printf("Enter an interger for #1:");
scanf("%d", a);
printf("Enter an interger for #2:");
scanf("%d", b);
printf("Enter and interger for #3:");
scanf("%d", c);
}
Upvotes: 0
Reputation: 4203
You could just use pointers. I.e., change the function declaration to:
int input(int *first, int *second, int *third);
In the function itself, you use:
int input(int *first, int *second, int *third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
And when you call it, use:
input(&first,&second,&third);
Upvotes: 0
Reputation: 1314
You can use pointers and pass the address of those variables into each function. Every time you use it, you can dereference each variable in the functions.
Upvotes: 0
Reputation: 3230
Either use a struct
struct Foo {
int first, second, third;
}
struct Foo input() {
struct Foo foo;
printf("Enter an interger for #1:");
scanf("%d",&foo.first);
printf("Enter an interger for #2:");
scanf("%d",&foo.second);
printf("Enter and interger for #3:");
scanf("%d",&foo.third);
return foo;
}
Or pass pointers:
void input(int* first, int* second, int* third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
int main(){
int first,second,third;
int added;
//sub functions
input(&first,&second,&third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
Upvotes: 3