简体   繁体   中英

Calling functions from main in C

I have a problem regarding calling functions in C from main,I think I have this problem because I still don't get used to the procedural language but I can't figure this out at all.The error is "number undeclared(first use in this function)" and "number2 undeclared(first use in this function)".

Code:

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

 int add(int number,int number2){
  int answer;
  answer = number + number2;

  printf("Enter a number\n");
  scanf("%d",&number);

  printf("Enter another number\n");
  scanf("%d",&number2);

   printf("The result of those numbers are %d\n",answer);
}



int main()
{
    add(number,number2);

    return 0;
}

Change your main function to declare the variables there, like this:

int main() 
{
    int number = 1, number2 = 2;
    add(number,number2);

    return 0;
}

C requires you to "declare" each variable before you use it. This involves a statement that specifies at least the variable's name and its type. For instance:

int number;

Your compiler is complaining that you have not done this for either number or number1 .

Additionally, for the behavior of your program to be defined, you must assign a value to each variable before you use it. For example,

number = 1;

You do not do this, either, but although the compiler likely will warn you about that if you do not correct it, it likely will also be happy to do what you said and produce a program that exhibits undefined behavior.

this code is really not correct

int add(int number,int number2){
  int answer;
  answer = number + number2;

  printf("Enter a number\n");
  scanf("%d",&number);

  printf("Enter another number\n");
  scanf("%d",&number2);

   printf("The result of those numbers are %d\n",answer);
}

It accepts two numbers as input to the function. Adds them, then asks the user 2 enter some numbers and displays the result of adding the numbers passed int the function call

YOu mean either:

int add(){
  int answer, number,numer2;

  printf("Enter a number\n");
  scanf("%d",&number);

  printf("Enter another number\n");
  scanf("%d",&number2);

   printf("The result of those numbers are %d\n",answer);
}

or

int add(int number,int number2){
  int answer;
  answer = number + number2;



   printf("The result of those numbers are %d\n",answer);
}

and put the prompting in main

main()
{
int number,number2;
  printf("Enter a number\n");
  scanf("%d",&number);

  printf("Enter another number\n");
  scanf("%d",&number2);
  add(number,number2)
}

you don't need conio.h

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

int add(int number,int number2) {
    return number + number2;
}

int main()
{
    int answer;
    int number, number2 = 0;

    printf("Enter a number\n");
    scanf("%d",&number);

    printf("Enter another number\n");
    scanf("%d",&number2);

    answer = add(number,number2);

    printf("The result of those numbers are %d\n",answer);

    return 0;
}

Part 1: You need to have variables declared before you use them.

In your main you do this add(number,number2); but how does main know what your variables are? You need to declare them:

int number, number2;

Part 2: Make your functions simple

You want a function to add 2 numbers, so that is what the function should do:

int add(int number1, int number2) {
    return number1 + number 2;
}

Part 3: Lets take some user input and add them

You shouldnt have your add function taking your user input, either make a new function like:

int getNumberFromUser(const char *promptText) {
    int userInput;
    printf("%s:", promptText);
    scanf("%d", &userInput);
    return userInput;
 }

Or just get them in main:

int main() {
  // Declare your numbers.
  int answer;
  int number1, number2;

  // Get input from the user.
  printf("Enter a number\n");
  scanf("%d",&number);
  printf("Enter another number\n");
  scanf("%d",&number2);

  // Add them together:
  answer = add(number1, number2);

  // Tell the user.
  printf("The result of those numbers are %d\n",answer);
}

Here is a live example.

There are several misconceptions present in your code.

First, the parameters to add() are not used as parameters. They are used as local variables. Instead, write the function explicitly indicating that:

int add()
{
  int answer, number, number2;
  ...

Secondly, answer is computed from the parameters, not from the values read. While that is computationally valid, it is confusing. What do you want this logic to really do? Here, I have moved the answer computation to after the inputs are read:

  printf("Enter a number\n");
  scanf("%d", &number);

  printf("Enter another number\n");
  scanf("%d", &number2);

  answer = number + number2;
  printf("The result of those numbers are %d\n",answer);

This ignores the parameters originally passed to add() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM