How to do operations between functions in C?

Asked

Viewed 129 times

1

The function funA() stores a value entered by the user. The function funB() will allow the user to choose an option. My difficulty is in the third function: I need to create a function that multiplies the value stored in the first function by the value generated by the user’s choice. I don’t know how to call these functions within the C function and use the stored values.

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

void funA(){

    int numA;
    scanf("%d", &numA);
}


void funB(){
    int numB;
    float x;

    printf("Escolha um valor:\n");
    printf("1 - Alto\n");
    printf("2 - Medio\n");
    printf("3 - Baixo\n");

    scanf("%d", &numB);

    if(numB == 1){x = 100;}
    if(numB == 2){x = 50;}
    if(numB == 3){x = 10;}

}
void funC(){
//?????
}

2 answers

3


You have two functions, funcA() and funcB() independent, everything that occurs within them ceases to exist when they are finished running. You can do two things:

1) Make them return something using return (however they may not be void)

Ex.:

int funA(){
    int numA;
    scanf("%d", &numA);
    return numA;
}

float funB(){
    int numB;
    float x;

    printf("Escolha um valor:\n");
    printf("1 - Alto\n");
    printf("2 - Medio\n");
    printf("3 - Baixo\n");

    scanf("%d", &numB);

    if(numB == 1){x = 100;}
    if(numB == 2){x = 50;}
    if(numB == 3){x = 10;}

    return x;
}

void funC(){
    int a = funcA();
    float b = funcB();
    float c = a + b;
    printf("Resultado: %.2f", c);
}

2) Sending as parameter for both the pointer for variables created in the funcC(), or global variables, if you have. For this, study pointers and passing parameters by value and by reference.

  • Got it! Thank you so much!

2

The variables in question exist only within their respective functions, they are local variables, and it is not possible to manipulate them from outside the functions. To solve your problem there are two alternatives.

1. Declare variables as global:

#include <stdio.h>

    int numA;
    float x;

    void funA(){

        scanf("%d", &numA);
    }


    void funB(){

        printf("Escolha um valor:\n");
        printf("1 - Alto\n");
        printf("2 - Medio\n");
        printf("3 - Baixo\n");

        scanf("%d", &numB);

        if(numB == 1){x = 100;}
        if(numB == 2){x = 50;}
        if(numB == 3){x = 10;}

    }

    void funC(){

        printf("%f", x * numA);
    }

2. Or change the type of the return of functions and manipulate them:

#include <stdio.h>

int funA(){
    int numA;

    scanf("%d", &numA);
    return numA;
}


float funB(){
    int numB;
    float x;

    printf("Escolha um valor:\n");
    printf("1 - Alto\n");
    printf("2 - Medio\n");
    printf("3 - Baixo\n");

    scanf("%d", &numB);

    if(numB == 1){x = 100;}
    if(numB == 2){x = 50;}
    if(numB == 3){x = 10;}
    return x;

}
void funC(){
    printf("%f", funA() * funB());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.