0

code:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

// PROTOTYPE THE FUNCTONS & DECLARE VARIABLES

int multiply(int ,int);

void get_input(int , int);

    int input1;
    int input2;
    int answer;

// RUN THE MAIN PROGRAMME

int main(void)

{

        // EXECUTE GET_INPUT FUNCTION

        get_input(input1, input2);

        // EXECUTE MULTIPLY FUNCTION

        multiply(input1, input2);

}


    // DEFINE GET_INPUT FUNCTION

    void get_input(input1, input2)
    {

        printf("give me 2 integers\n");

        do
        {

        input1 = get_int("Integer 1: ");
        input2 = get_int("Integer 2: ");

        }

        while ((input1 || input2) <= 0);

    }

    // DEFINE THE MULTIPLY FUNCTION

    answer multiply(input1, input2)

    {

        int answer = (input1 * input2);

        printf("%i", answer);

    }

errors:

~/Week_2/Practice/ $ make functionpractice
clang -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    functionpractice.c  -lcrypt -lcs50 -lm -o functionpractice
functionpractice.c:36:20: error: declaration shadows a variable in the global scope [-Werror,-Wshadow]
    void get_input(input1, input2)
                   ^
functionpractice.c:13:9: note: previous declaration is here
    int input1;
        ^
functionpractice.c:36:28: error: declaration shadows a variable in the global scope [-Werror,-Wshadow]
    void get_input(input1, input2)
                           ^
functionpractice.c:14:9: note: previous declaration is here
    int input2;
        ^
functionpractice.c:55:5: error: unknown type name 'answer'
    answer multiply(input1, input2)
    ^
functionpractice.c:55:21: error: declaration shadows a variable in the global scope [-Werror,-Wshadow]
    answer multiply(input1, input2)
                    ^
functionpractice.c:13:9: note: previous declaration is here
    int input1;
        ^
functionpractice.c:55:29: error: declaration shadows a variable in the global scope [-Werror,-Wshadow]
    answer multiply(input1, input2)
                            ^
functionpractice.c:14:9: note: previous declaration is here
    int input2;
        ^
functionpractice.c:59:13: error: declaration shadows a variable in the global scope [-Werror,-Wshadow]
        int answer = (input1 * input2);
            ^
functionpractice.c:15:9: note: previous declaration is here
    int answer;
        ^
6 errors generated.
<builtin>: recipe for target 'functionpractice' failed
make: *** [functionpractice] Error 1
0
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

// PROTOTYPE THE FUNCTONS

int get_input();

int multiply(int input1, int input2);

// RUN THE MAIN PROGRAMME

int main(void)  {

            //DECLARE THE VARIABLES WITHIN THE MAIN PROGRAMME FUNCTION FOR USE IN OTHER FUNCTIONS WITHIN MAIN

            int input1;
            int input2;

            printf("give me 2 positive integers\n");

                do {

                    input1 = get_int("Integer 1: ");
                    input2 = get_int("Integer 2: ");

                    }

            while ((input1 || input2) <= 0);

            // EXECUTE MULTIPLY FUNCTION

            multiply(input1, input2);

            int result = multiply(input1, input2);

            printf("the answer to %i x %i = %i\n", input1, input2, result);

            }











// DEFINE THE MULTIPLY FUNCTION

int multiply(int input1, int input2)

{

    int answer = (input1 * input2);

    return answer;

}

Got there in the end.

basically i was trying to do too much via functions and left essentialy nothing in "main". its not possible to globally declare my variables and call them into use in functions outside of main.

Main things I need to remember:

  • The compiler strictly reads from top to bottom in regards to user inputs and variables.

  • Function outputs are best reassigned within main for further use.

| improve this answer | |
-1

You need define a name for arguments and not just data types.

In the function prototype, you will declare it like this:

int multiply(int input1, int input2);

For the function definition, you copy the function prototype remove the semicolon and add curly braces like this:

int multiply(int input1, int input2)
{
    //Code here
}
| improve this answer | |
  • unfortutely the didnt help. the reason why i changed the function to how it is now is because i repeatedly fouind the error "declaration shadows a variable in the global scope". maybe you can explain this error so i may better understand my mistkaes trying to compile this code. – Pot Noodle Jun 6 at 6:58
  • It is because because you declared your variables out of any function. Global variable means that it's accessible anywhere. To silence that warning, you need to declare your variables inside the main function and also move the contents of get_input to the main function and pass the input parameters to the multiply function or you can put your variables in the get_input function and just call it in the main function with out any arguments. – Gio Sauquillo Jun 6 at 10:50
  • Also, take note of your multiply function. The first thing you write in every function is its return type. If you will not return something to the object that called the function just put void. – Gio Sauquillo Jun 6 at 10:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .