Functions passing to functions

This is a discussion on Functions passing to functions within the C++ Programming forums, part of the General Programming Boards category; Hello all, It maybe that I have thought about this and stared at it too long. I don't want the ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Question Functions passing to functions

    Hello all,
    It maybe that I have thought about this and stared at it too long. I don't want the answer just the idea. I have a problem that is all functions. The first function gathers three sperate ints. Ask for the employee ID. Ask for the plan they want
    (1,2 or 3). Then their number of dependents. This is all in one function. How do I get the three ints from this function back to the main without reference variables. Then a function to gather this information together and display it. I had it written and then I looked again and it was wrong. I was soing it without some of the functions. I will look at it again later, maybe it will come to me. Fatigue has the better of me now. 12 hour of thinking and doing it wrong once has taken its toll.
    Thanks,

    I have looked every where.

  2. #2
    Registered User
    Join Date
    Jul 2010
    Posts
    55
    Since you don't want reference variables maybe you could pass a struct with those 3 variables by value and return it to main

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    It's an asignment and we haven't touched structs, I was going to do just that, then he would probably fail it.

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    edit: The above 2 post were made while I was typing this one, and are based on assumptions(such as the ability to use structs) later ruled out.

    The C way would be to make at struct:
    Code:
    struct employee{
       unsigned int id;
       unsigned char plan;
       unsigned int dependents;
    };
    but to only define the struct in the .c, typedefing in the .h. Then to make a bunch of functions for manipulating employee_t's(eg. A function for getting id, another for getting plan, etc., one for init'ing one for fini'ing.). The C++ way would be to make an employee class and methods for manipulating it.

    I suggest you learn the C way and it's faults then when you try to lean C++, you'll know why it's OOP is a god send.

    I've attached a simple array lib I've been working on lately, it uses the C-style model. Notice how it is possible to seamlessly use the array_t type w/o ever needing to know what it really consists of.
    Attached Files Attached Files
    Last edited by User Name:; 09-05-2010 at 04:50 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Quote Originally Posted by hausburn View Post
    It's an asignment and we haven't touched structs, I was going to do just that, then he would probably fail it.
    Then you're SOD.
    Take a pick. References or struct.
    Otherwise you can't return 3 values at once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    OK, real programmers please look away... for an assignment... you are probably expected to use global variables. But please don't try this at home or in a real application.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #7
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Ewww... global variables... nvgoit's probably right.

  8. #8
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Quote Originally Posted by User Name: View Post
    Ewww... global variables... nvgoit's probably right.
    Urgh. Probably.

    Quote Originally Posted by hausburn View Post
    It's an asignment and we haven't touched structs, I was going to do just that, then he would probably fail it.
    If your instructor/teacher/professor/what-have-you fails you because you used something you've learned about on your own but it hasn't been officially introduced by the course, and the assignment requirements didn't specifically state that, no, you can't use anything not covered so far, well, I'd file a formal complain with the faculty. Because it's BS.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Sometimes there is a reason, actually. I know from one of my own course which was an obligatory introduction to programming. Every week, students had to review other students' code and grade it. To give some experience in looking at others code and so. But since there were students who were new to programming, using advanced features not yet learned was forbidden.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    I've had professors forbid me from using anything not yet discussed in class. He saved structs for the last of the semester. It's best just to do what the professors say. Otherwise, you wont like having points deducted from assignments regardless of if there is a better way to do something. Professor wanted me to implement bubble sort exactly by his algorithm. Mine was better so I used mine, got points deducted.

  11. #11
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    To be fair, if you are in a company as a programmer and they want you to implement the algorithm in the worst (for you) way, you implement it in the worst way. Simple as that. A requirement is a requirement. It is part of the job. If you want to change the requirements that is another story. The same is with the professor. If he specifically asks you to, why change it? If you want to change it ask him/her in advance.

    The same with not using things that are not introduced in the class. Maybe the professor wants to teach you another way as well to do things. He should find an example that you wouldn't use things that are not introduced, but, hey, it is simpler just to restrict you. If he wanted to teach you how to read a line and split it with a delimiter using only getchar() and array manipulation then it would make no sense if you just used fgets() and strtok() instead, would it?

    Eh, global variables maybe have a lot of downsides but that doesn't mean they shouldn't be used. Practically they can be used very safe with proper planning and naming. For example
    Code:
    int MyWhateverFunctionCounter = 1;
    void MyWhateverFunction() {...}
    You are using a global variable in one function, there is no real confusion or anything. The same for using one flag to synchronize threads etc etc.
    Now, if you were to do things like
    Code:
    int result;
    
    int main()
    {
        ...
        result = x * y;
        AddToResult(100);
    }
    where AddToResult() updates the global variable result then you have a MESS.

    But I would prefer instead of avoiding global variables like the plague to teach people about using them in one logic unit. Because you don't have classes in C to limit the scope of them doesn't mean that you cannot use them. You just have to assign them yourself on a logic unit (like 4 functions that are used for a single logical operation) and not use them out of that.

    If you avoid something you never learn about it. There are some things that you can avoid entirely (like gets() or goto) but finding a way to avoid a global variable doesn't mean that your code will be better. It might as well become more complicated. You might as well waste time trying to find a workaround instead of focusing on more important parts.

  12. #12
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    265
    Those instances where I end up passing a variable to every single function are the times that I use global variables.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,985
    Just accept that every function needs that variable. If there are many, make a struct.
    There are times and places for everything, but globals are best avoided for most parts.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing pointers to functions in seperate source files
    By derekeverett in forum C Programming
    Replies: 12
    Last Post: 10-08-2009, 08:15 AM
  2. Passing 2D arrays between functions
    By taurus in forum C Programming
    Replies: 10
    Last Post: 09-28-2009, 06:05 AM
  3. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 09:08 AM
  4. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 12:22 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21