-2

I am trying to call a function from the main function, but it gave me a random number when I compiled and ran it. I want to ask from the user to enter a number in the yrBorn function, then return it to the main function and finally display it in the terminal.

int yrBorn(int);
#include<stdio.h>
int yrBorn (int yearBorn)    
{   
    printf("Enter the year you were born in: ");
    scanf("%d", &yearBorn);
    return yearBorn;
}

int main ()
{       
    printf("%d",yrBorn);
}
Suraj Jain
  • 4,463
  • 28
  • 39

4 Answers4

0

You have to understand your code like this :

int yrBorn(int);  // function prototype say it must accept a int argument
#include<stdio.h>
int yrBorn (int yearBorn) //it must accept a argument of int type because of this definition    
{   
    printf("Enter the year you were born in: ");
    scanf("%d", &yearBorn);
    return yearBorn;
}

int main ()
{       
   printf("%d",yrBorn); // You are passing function pointer here not a function call use like yrBorn(1994)
    printf("%d",yrBorn(1994)); //it will work :)
}
0
#include<stdio.h>

int yrBorn();
int yrBorn()    
{   
    int yearBorn = 0;
    printf("Enter the year you were born in: ");
    scanf_s("%d", &yearBorn);
    return yearBorn;
}

int main()
{
    printf("%d", yrBorn());
}

Modified your program to work as you expect!

mac
  • 81
  • 5
0

Your Syntax is not correct.

Suppose For Example , I want to make a Function That takes a number and returns it square , I would go like ,

#include<stdio.h>

int square(int);    //Function Prototype Declaration Telling Compiler  
                    //That there would be function named square whose        
                    //takes an integer and whose return type is integer.

int main()

{
   int x = 4;
   int y ;
   y = square(x);          //Calling Square Function passing Value `x`
                           //And Storing it in `y`.
   printf("%d\n" , y);     //Printing `y`

   //Or You Could As Well Do This
   printf("%d" , square(x));      //Without Storing Printing return
                                  //value of square(x)

  return 0;
 }


 int square(int k){   //Function Definition Contains The Code What The      
  int r = k*k;        //Function Does , Here it take the value passed      
  return r;           //to the function in this case `x` and store it 
                      //in `k` , and then initialise `r` to be `k*k` 
  }                   //and then returns `  

So in your code

  int yrBorn(int);
  #include<stdio.h>
  int yrBorn (int yearBorn)    
  {   
      printf("Enter the year you were born in: ");
      scanf("%d", &yearBorn);
      return yearBorn;
  }

   int main ()
   {       
     printf("%d",yrBorn);      //Here You are not calling function.
     printf("%d" , yrBorn(0)); //This will work as in your prototype
                               //declaration you have declared
                               //function to take a int parameter and 
                               //return int
   }

What You Would Wanted I Suppose Was this

  #include<stdio.h>
  int yrBorn(void);
  int yrBorn (void)   //Here Function is Expected to take no arguments 
                      //and return int value.
  {
    int yearBorn;
    printf("Enter Your Birth Year : ");
    scanf("%d" , &yearBorn);
    return yearBorn;

   }    

 int main(){
     printf("%d" , yrBorn());     //You Would Call it like this 
   }

Notice there is nothing between parenthesis it is because function does not accept any arguments.

Please Read A Decent C Book with complete dedication forgetting everything what you already know or you would face similar misconceptions later too , I would recommend Watching this series Nptel - Introduction to C Programming and checking this The Definitive C Book Guide and List for Best C Programming Books For Beginners.

Community
  • 1
  • 1
Suraj Jain
  • 4,463
  • 28
  • 39
0
printf("%d",yrBorn);

This is not how a function is called. What you're doing instead of calling yrBorn is taking its address. That address is then being interpreted as an int and printed as such.

To call a function, you need to use the () operator. Any parameters to the function would go inside the parenthesis.

Also, your function is taking a parameter, but isn't doing anything with its value. Rather than take a parameter, this variable should be local to the function.

So with these fixes, your function would look like this:

int yrBorn ()    
{   
    int yearBorn;   // local instead of a parameter
    printf("Enter the year you were born in: ");
    scanf("%d", &yearBorn);
    return yearBorn;
}

And you call it like this:

printf("%d",yrBorn());
dbush
  • 205,898
  • 23
  • 218
  • 273