开发者

Programming with functions using C. The functions are not executed

I'm learning C and doing the exercise of the function chapter. So i have written a small programm with 3 files and two small functions. Since开发者_如何学运维rly it does not work. I have no errors, the functions are simply not executed and i don't know why.

First of all this is my headerfile which only declares my functions.

//employee.h
int addEmployee(void);
int printEmployee(int i);

So the next file is for the definition of the functions.

//employee.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>

int numE;
int i;

int addEmployee(void)
{
    printf("Please type in the number of your employees: ");
    scanf_s("%d", &numE);
    i = numE;
    return i;
}

int printEmployee(int i)
{
    printf("%d", i);
    getchar();
    getchar();
    return i;
}

And the last file is used to execute the functions.

//lab6.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>


int main ()
{
    int addEmployee();
    int printEmployee();
    return 0;
}


Using

int main ()
{
    int addEmployee();
    int printEmployee();
    return 0;
}

you're basically declaring 2 new functions with 0 arguments.

You're not calling your "old" functions.

This should work, as others have pointed out:

int main ()
{
    int emp = addEmployee();
    printEmployee(emp);
    return 0;
}

Because you're calling addEmployee(), storing it's result to emp and then printing emp using printEmployee. Since printEmployee is declared with one parameter, you just put emp in and it will work.


When you call a function you do not put the return type in front of the call. The call is simply the name of the function and any parameters you are calling it with. So your main function should look like this:

int main() {
    addEmployee();
    printEmployee(1);

    return 0;
}

EDIT: So in your employee.c file, you are trying to use addEmployee() to take a number of employees from the command line and store it in the variable i right? And you want printEmployee() to tell you how many employees were entered? Here's how you would do that.

//employee.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>

int i;

int addEmployee(void)
{
    int numE;
    printf("Please type in the number of your employees: ");
    scanf_s("%d", &numE);
    i = numE;
}

int printEmployee()
{
    printf("%d", i);
    getchar();
    getchar();
}

Here's what I did.

First, I made numE a variable local to the addEmployee function that uses it. Generally you should keep variable scope as small as possible. That means keep them down to the lowest level they are used. In this case, numE is only needed by addEmployee() so that's its scope.

Second, I removed the parameter from int printEmployee(int i). It was overriding your i variable at the file level. So you were storing the number read into numE in i but then when you entered printEmployee() you were creating a new, empty i that hid it. When you called printEmployee(1) from main, you were passing the value 1 into i in printEmployee(int i). By removing the parameter, you stop hiding employee.c's i.

Finally, I removed the returns. A function doesn't have to return anything in C. And if you are not going to use the return, then it's just an extra line of code to include it.

There's one more change you'll have to make to make this work, in your lab6.c file. Remove the parameter from the call to printEmployee()

//lab6.c
#include "employee.h"
#include <stdio.h>
#include <ctype.h>


int main ()
{
    addEmployee();
    printEmployee();
    return 0;
}

Should work the way you expect it now.


you want:

int main () 
{ 
    addEmployee(); 
    printEmployee(1); 

    return 0; 
} 


Change this:

int main ()
{
    int addEmployee();
    int printEmployee();
    return 0;
}

To this:

int main ()
{
    addEmployee();
    printEmployee();
    return 0;
}

You're re-declaring the functions instead of calling them.

You'll also have to change your printEmployee function to not accept an integer argument; it seems like it should just be using the same global variable as addEmployee. This is a bad idea though; global variables are generally to be avoided. addEmployee should probably return the employee ID, which you could store and then pass into printEmployee.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜