0

I'm very new to programming and recently I came across the function topic. As I wanted to experiment out my knowledge I wrote this very basic code

My aim is to make the separate read function, calculate function and print function and to call the three function in the main... However, this code doesn't show any error but the output that it gives is just the garbage value, how can I make it to really work?

#include <stdio.h>
#include <stdlib.h>
void read(int hrs,int pR);
int calc(int hrs, int pR);
void print(int res);

int main()
{
    int hours, payRate;
    read(hours,payRate);
    calc(hours, payRate);
    int c = calc;
    print(c);
}

void read(int hrs,int pR)
{
    printf("enter hours and pay rate");
    scanf("%d%d", &hrs, &pR);
}

int calc(int hrs, int pR)
{
    int grossPay = hrs * pR;

    return grossPay;
}

void print(int res)
{
    printf("The grossPay is %d", res );
}

3 Answers3

1

calc(hours, payRate); here you call the function, but discard the return value. Good compilers will warn about this. Should have been

int c = calc(hours, payRate);

In addition, the function read only modifies local copies of the variables. The values stored are not returned to the caller. You will have to change the function so that it takes the address of the variables allocated by the caller:

void read(int* hrs, int* pR)
{
    printf("enter hours and pay rate");
    scanf("%d%d", hrs, pR);
}

Now the real troubling part here is that the line int c = calc; compiled without giving you a message from the compiler. Which means your compiler is not working correctly, since this line is not valid C. calc is a function (that decays into a function pointer), which can't be assigned to an integer - this is not allowed by the language.

Some non-standard compilers would treat that line as calc decays into a function pointer, which is then assigned to an integer. To prevent such dangerous behavior, make sure to get a C compliant compiler.

I recommend using gcc, which can be used to compile standard C code, if you call it like this:

gcc -std=c11 -pedantic-errors
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

First, your read function has nowhere to store the results that it reads. Since yours hrs and payrate are declared in main, the read() function cannot store into those variables. Also, your calc() function returns a result as it should, but your name is not using it. call read this way:

 read( &hours, &payrate);

then the read method is:

void read(int *hrs,int *pR)
{
printf("enter hours and pay rate");
scanf("%d%d", hrs, pR);
}

In main use the result of calc() like this:

int c = calc(hours, payRate);
print(c);
-1

try to pass value by reference

#include <stdio.h>
#include <stdlib.h>
void read(int *hrs,int *pR);
int calc(int hrs, int pR);
void print(int res);

int main()
{
int hours, payRate;
read(&hours,&payRate);
int c = calc(hours, payRate);
print(c);
}

void read(int *hrs,int *pR)
{
 printf("enter hours and pay rate");
 scanf("%d%d", &*hrs, &*pR);
}

int calc(int hrs, int pR)
{
 int grossPay = hrs * pR;

 return grossPay;
 }

void print(int res)
{
   printf("The grossPay is %d", res );
 }
JBR
  • 136
  • 9