Solving a C Local Variable Issue

  • Thread starter Thread starter Nothing000
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the issue of passing local variable values between functions in C programming. Participants are exploring how to return values from one function to another and how to properly handle function parameters and return types.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses confusion about how to return a local variable's value from function f1 to main.
  • Another participant asks for clarification on the specific problem being encountered with the code provided.
  • There is a mention of needing to pass the variable a to function f2, raising questions about how to do so correctly.
  • A suggestion is made to declare functions with appropriate return types and to assign returned values to variables in main.
  • One participant points out that the returned value from f1 should be stored in a variable when called.

Areas of Agreement / Disagreement

Participants do not reach a consensus, as there are multiple competing views on how to correctly implement the function calls and variable passing in C.

Contextual Notes

There are unresolved issues regarding the correct handling of variable types and function parameters, as well as potential misunderstandings about how to use the scanf function properly.

Who May Find This Useful

Individuals learning C programming, particularly those interested in function handling and variable scope.

Nothing000
Messages
403
Reaction score
0
Will someone please help me. I can not fiugure out how to get a local variables value to go from one function to another. Here is the source code:

#include <stdio.h>

int f1(int b);


int main()
{
int a = 0;
int b = 0;

f1();
printf("The value returned TO main() FROM f1() is:\n", a);



return 0;
}

int f1()
{
int a = 0;

printf("Enter a whole number.\n");
scanf("%d", a);

return a;
}
 
Computer science news on Phys.org
How do I return the value a back to the main function.
 
What problem are you having? Does the above not work?
 
The second function asks for input by the user and stores that as the variable a. But how do I get the value of a back to the main function.
 
The big question I have is this: How would I get the variable a to go to the function f2 in this
#include <stdio.h>

int f1(int );
int f2(int );

int main()
{
int a = 0;
int b = 0;

f1();
printf("The value returned TO main() FROM f1() is:\n", a);
f2();
return 0;
}int f1()
{
int a = 0;

printf("Enter a whole number.\n");
scanf("%d", a);

return a;
}


int f2()
{
printf("The value you entered for a is:%d\n", a);
}
 
Can anyone help me?
 
Declare your function as returning int, float, etc. Then assign it to a variable of that type. For f2, declare it as taking a parameter of appropriate type, and then pass the argument to it. In C++

Code:
#include <iostream>

int f1();
void f2( int arg);

int main()
{
    int a = f1();
    f2(a);
    return 0;
}

int f1()
{
    return 42;
}

void f2( int arg)
{
    std::cout << "f1 returned " << a << std::endl;
    return;
}

Good luck,
Tim
 
Oh, I see the problem, when you call the function f1, you need to put the returned value somewhere when it comes back.

int x = f1();
printf("The value returned TO main() FROM f1() is:\n", x);

I see nmtim has already addressed that.
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K