Klayd Pro
Klayd Pro

Reputation: 11

How to call a local variable from another function c

I am new to programming. I would like to call from functions(first, second, third the variables in function "enter" and in function "math" accordingly).

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void math (void);
void first (void);
void second (void);
void third (void);
void enter(void);
void scan(void);


int main() 
{

 first();  //function
 second(); //function
 third();  //function
 enter();  //function
 printf("\n\n Would you like to edit? (Y/N)\n\n");
 scan();   //function
 return(0);
}

Here is the user input:

void first (void){
float a;
printf("Enter First number: ");
scanf("%f",&a);
 }

 void second (void){
 float b;
 printf("Enter Second number: ");
 scanf("%f",&b);
  }

  void third (void){
  float c;
  printf("Enter Third number: ");
  scanf("%f", &c );
  }

How can I call the variables from the user input to the function below?

  void enter(){
  float a,b,c;
  printf("you have entered a: %f, b: %f, c:%f", a,b,c);
     }

This is the loop in case the user would like to edit or continue with the initial input numbers.

    void scan(void){
    int ch;
    scanf("%d", &ch);
    if (ch==1){
        main();

        }else{
        math();
        }
        }

Here I would also like to call the input variables from function (first, second third) in order to perform some math calculations.

        void math (void){
           float a,b,c;
           float x,y,z;
           x=a+b+c;
           y= powf(x,2);
           z=sqrt(y);

           printf("Final Result of Addition is: %f \n Final Result of 
           Multiplication is: %f \n Final Result of squareroot  is:%f\n 
           ",x,y,z);
                          }

Upvotes: 0

Views: 7716

Answers (3)

R Sahu
R Sahu

Reputation: 206637

It's best if functions don't depend or modify data external to them. It's not always possible but it is better if they can be defined with that goal in mind.

In your case, the functions first, second, and third are doing essentially the same thing. It will be better to use just one function and give it a more appropriate name, such as read_float and change the return type so that the value input by the user is returned to the calling function.

Declare it as:

float read_float(char const* prompt);

Then, you can use it as:

float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");

etc.

The function enter does not correctly convey what it is doing. It should be renamed to something more appropriate, such as display_input_values. It can accept the values input by the user as its arguments.

Declare it as:

void display_input_values(float a, float b, float c);

and use it as:

float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");
display_input_values(a, b, c);

The scan function does not convey its meaning clearly either. You could divide scan to two functions -- one that returns you the option of whether to continue with the next inputs or to call a function that computes something with the user input.

You can get the option from the user by using a function named read_int function as:

int option = read_int("Enter 1 for next round of inputs and 2 to compute with current input:");

and use that returned value as:

if ( option == 2 )
{
   // Do the computations
}

You also need another option to exit the program. The call to get the option needs to be:

char const* prompt = "Enter 1 for next round of inputs.\n"
                     "Enter 2 to compute with current input.\n"
                     "Enter 3 to exit program.\n";
int option = read_int(prompt);

Always add code to check whether scanf succeeded before proceeding to use the data that you were expecting to read into.


Here's an updated version of your code.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float read_float(char const* prompt);
int read_int(char const* prompt);
void display_input_values(float a, float b, float c);
void math(float a, float b, float c);

int main() 
{
   while ( 1 )
   {
      float a = read_float("Enter first number: ");
      float b = read_float("Enter second number: ");
      float c = read_float("Enter third number: ");

      display_input_values(a, b, c);


      char const* prompt = "Enter 1 for next round of inputs.\n"
                           "Enter 2 to compute with current input.\n"
                           "Enter 3 to exit program.\n";
      int option = read_int(prompt);
      if ( option == 3 )
      {
         break;
      }

      else if ( option == 2 )
      {
         math(a, b, c);
      }
   }
}


float read_float(char const* prompt)
{
   float num;
   printf("%s", prompt);

   // For flushing the output of printf before scanf.
   fflush(stdout);

   if (scanf("%f", &num ) != 1)
   {
      print("Unable to read number. Exiting.\n");
      exit(1);
   }
   return num;
}

int read_int(char const* prompt)
{
   int num;
   printf("%s", prompt);

   // For flushing the output of printf before scanf.
   fflush(stdout);

   if (scanf("%d", &num ) != 1)
   {
      print("Unable to read number. Exiting.\n");
      exit(1);
   }
   return num;
}

void display_input_values(float a, float b, float c)
{
  printf("You have entered a: %f, b: %f, c:%f\n", a, b, c);
}

void math(float a, float b, float c)
{
   // Do whatever makes sense to you.
}

Upvotes: 3

AxW
AxW

Reputation: 1

For example declare float a,b,c; in your Main and change your functions to return a float float first() { ... } Then writea = first();

Upvotes: 0

bebidek
bebidek

Reputation: 592

Local variables (as name suggests) are local and can't keep their values after function ends. You have two options:

  1. Make functions first, second and third return their numbers and another functions get them as arguments.
  2. Make a,b,c global variables.

The second one is much easier, but it's so called bad programming style.

Upvotes: -1

Related Questions