Home > Blockchain >  How can I properly use While inside a Function?
How can I properly use While inside a Function?

Time:03-29

First of all I'm quite new to programming and this is my first question here. I'm creating a code to calculate the trapezoid's area, and I need to make the same while() 3 different times to check if the number is greater than zero, and in case it isn't, It'll keep asking the number until it is. I then decided to create a function() in order to make the code clean and less repetitive, the thing is, I'm probably doing something wrong, since I only get the negative number returned to the variable.

I took part of the code to show you guys and also to test. I'm always typing a negative number first in order to activate the while() inside function(), then after that I type a positive number, but I get the negative number printed instead of the new number. Any tips on how to get the new number in the largeBase variable? Here's the code:

#include <stdio.h>

int checkBelowZero(float x);

int main() {

  float largerBase, x;

  printf("\n\t\tTrapezoid's area calculation\n\n");
  printf("Type the trapezoid's larger base: ");
  scanf("%f", & largerBase);

  checkBelowZero(largerBase);

  printf("%.2f", largerBase);

  return 0;
}

int checkBelowZero(float x) {

  while (x <= 0)

  {
    printf("\nThe number has to be greater than zero (0).\n\nPlease, type it again: ");
    scanf("%f", & x);
  }
  return x;

}

CodePudding user response:

Solution 1

Function parameters are copies of the value that they are given. If you modify a parameter's value, you are not actually modifying the original variable that was passed.

However, this is not true when passing by reference. If you pass the address of a variable to a function, and then call a pointer to that you can modify the true value of the variable that was passed. I have re-written the function in question so you can see how it works:

// the parameter is expecting a pointer to something
void checkBelowZero(float *x) {
   /*- to read the parameters value, call a pointer to it
     - "x" is actually just a number which is the memory 
     location of the variable you passed. Calling a pointer
     to it reads what's at that address, in this case the 
     value of your variable "largerBase".
   */
  while (*x <= 0)

  {
    printf("\nThe number has to be greater than zero (0).\n\nPlease, type it again: ");
    /* - Since "x" already contains the base address of
       "largerBase", you shouldn't call the base address of
       that
    */
    scanf("%f", x);
  }

}

Make sure to modify the prototype

void checkBelowZero(float *x);

And call the function as so

checkBelowZero(&largerBase);

This makes sure to pass the base address of our variable.


Solution 2

Since you are returning the value x you can actually set the value of largerBase to the return value of the function.

largerBase = checkBelowZero(largerBase);

Keep in mind, x does not need to be defined as a variable. As it is a parameter only accessible to the function it belongs to

  • Related