-1

The title describes what I'm trying to do, but I'm getting the error message that I never declared base1. I actually know this, but I'm not exactly sure how to actually fix the problem.

int getBase1(void);
int setBase1(double);

int main(void){
    getBase1();
    setBase1(base1);
}

int getBase1(void){
    printf("Please enter the length of a base: ");
    return;
}

int setBase1(double base1){
    scanf("%lf", &base1);
}
user147219
  • 355
  • 1
  • 4
  • 13
  • 3
    The compile is telling you that `base1` name is not defined inside the scope of `main`, which is entirely correct. You probably want to elaborate more on what you want to achieve. – norok2 Sep 19 '17 at 13:25
  • Why not pass a reference for the variable? scanf will set value for that reference. – Divya Mamgai Sep 19 '17 at 13:26
  • 3
    @DivyaMamgai there are no references in C. – Jabberwocky Sep 19 '17 at 13:26
  • because base1 is not defined – MCG Sep 19 '17 at 13:28
  • Hmmm. Both `getBase1()` and `setBase1()` should return `int`s, by their function prototypes, yet neither return value is collected in `main()`. Further, `getBase1()` has an empty `return` statement, and `setBase1()` has no `return` statement. – ad absurdum Sep 19 '17 at 13:31
  • @Michael Pointers can be used instead of references to achieve similar but not exact functionality. – Divya Mamgai Sep 19 '17 at 13:33
  • 1
    @DivyaMamgai if you mean "pointer" then you should write "pointer". And in C you cannot pass variables by reference as in C++. – Jabberwocky Sep 19 '17 at 13:38
  • @Michael My mistake :) – Divya Mamgai Sep 19 '17 at 13:39
  • You should tell us what you are _actually_ trying to achieve. This looks like an [XY Problem](http://xyproblem.info/). – Jabberwocky Sep 19 '17 at 13:43
  • Possible duplicate of [How to change variable?](https://stackoverflow.com/questions/3377158/how-to-change-variable) – phuclv Sep 09 '18 at 04:51
  • another duplicate: [Why function will not change variable?](https://stackoverflow.com/q/19483459/995714) – phuclv Sep 09 '18 at 04:54

3 Answers3

6

You must use pointer, otherwise the variable inside the method will not point to the same memory adress. Using pointer you'll be putting the value inside the memory address of the variable that you pass in the function call. One more thing, this way you will not need return values.

Try this way:

#include <stdio.h>
void getBase1(void);
void setBase1(double *base1);

int main(void){
    double base1;
    getBase1();
    setBase1(&base1);
    printf("%lf", base1);
}

void getBase1(void){
    printf("Please enter the length of a base: ");
}

void setBase1(double *base1){
    scanf("%lf", base1);
}
  • The function prototypes in this answer still say that these functions should return `int` values. Nitpicks: for robust code, the return value from `scanf()` should be checked; and while `%lf` is not wrong for printing `double` values with `printf()`, the `l` modifier is not needed, and has no effect on the `f` conversion specifier (but it is needed with `scanf()`). – ad absurdum Sep 19 '17 at 13:41
  • by the way `int main()`, should need a return type. You're not returning anything. – danglingpointer Sep 19 '17 at 13:42
  • 1
    @DavidBowling My bad man. Thanks for your atention. Corrected! – Gabriel Magri Sep 19 '17 at 13:42
  • @LethalProgrammer yes, corrected. I made use of his code to be fast, haha, sorry. – Gabriel Magri Sep 19 '17 at 13:43
  • 1
    `printf("%lf", base1);` -> `printf("%f\n", base1);` – chqrlie Sep 19 '17 at 13:44
  • 2
    @LethalProgrammer-- `main()` is a special case. No explicit `return` statement is needed here (since C99); in this case, a value of 0 is returned if the end of `main()` is reached without an explicit `return`. – ad absurdum Sep 19 '17 at 13:44
  • I'm just pointing out something you missed it, I see now you edited the answer, changed as void. – danglingpointer Sep 19 '17 at 13:45
  • @DavidBowling thanks for pointing out about return type of main. I wasn't aware that OP is using C99. – danglingpointer Sep 19 '17 at 13:46
  • @LethalProgrammer-- well, I assume that anyone is using at least C99 in 2017 unless they explicitly state otherwise ;) – ad absurdum Sep 19 '17 at 13:49
  • 1
    @GabrielMagri-- `int main(void)` was fine, as this is one of the two function signatures for `main()` which is sanctioned by the Standard. `void main(void)` is an implementation-dependent signature, which is not portable. – ad absurdum Sep 19 '17 at 13:50
  • Thanks you! Also, to use the variable again in a new function, would I just call it &variable_name? – user147219 Sep 19 '17 at 13:52
  • @user147219 your last question _to use the variable again in a new function, would I just call it &variable_name_ suggests that you really should read your C text book. – Jabberwocky Sep 19 '17 at 13:55
  • @user147219, if the other function use pointer too you can do this way. Other way is to pass as a non pointer parameter and use return value of the function to assign it again to the original variable. – Gabriel Magri Sep 19 '17 at 13:55
1

Seems like you're quite new to C programming. Here's a thing, you simply can't use scanf to modify a value of a main function variable without using pointers. If you read about scanf, you would find out that scanf requires the memory address of a variable; that's why scanf is able to read the format string and modify your variable. So what you're trying to achieve is pretty much similar to scanf, you have to pass the address of base1; first of all declare it! Since that's what compiler is crying about. Do the following things:

  1. Declare and pass the address of the variable you want to modify. Pass the address of base1 like this:

    double base1; getBase1(); setBase1(&base1);

  2. In the function getBase1 you're doing a void return, but your function signature tells the compiler that you would return an int. So your functions should look like this:

    void getBase1(void); void setBase1(double *);

  3. Since your setBase1 is receiving an address, there is no need for an ampersand(&). Simply pass the pointer value received:

void setBase1(double *pBase) { scanf("%lf", pBase); }

Rohan Kumar
  • 5,427
  • 8
  • 25
  • 40
0

You have many errors in your code, first int main(), should have the return type and your function doesn't return anything either. base1 is not declared.

error: ‘base1’ undeclared (first use in this function)
     setBase1(base1);
          ^

where is the base1 in your main function? Its basic your passing base1 as an argument to setBase1 but base1 is not declared.

danglingpointer
  • 4,708
  • 3
  • 24
  • 42