Question on Proper use of Functions - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Question on Proper use of Functions

Ed HallEd Hall Posts: 395Member
Please forgive me if my lack of formal programming education is the real trouble, but I see problems being asked on the board now and then where part of the problem states to call a function to do something that I can do as part of one line of code.

If one line of code can be used within main() to determine some result, would it still be "better" or perhaps more "proper" to send out to a function?

As an example, the recent post "coin flip in C++" states "the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads."

If I write this program with a function called flip, I have to add several lines. If I omit the need for the function flip, I can calculate the answer in one line plus the printf/cout line.

Is the need for a separate function just a part of this class structure at this point in its schedule or is it more proper to perform everything in separate functions? Would I be a problem student in this class?:-)

Comments please. Thanks.

Take Care,
Ed

Comments

  • shaolin007shaolin007 Posts: 1,018Member
    : Please forgive me if my lack of formal programming education is the real trouble, but I see problems being asked on the board now and then where part of the problem states to call a function to do something that I can do as part of one line of code.
    :
    : If one line of code can be used within main() to determine some result, would it still be "better" or perhaps more "proper" to send out to a function?
    :
    : As an example, the recent post "coin flip in C++" states "the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads."
    :
    : If I write this program with a function called flip, I have to add several lines. If I omit the need for the function flip, I can calculate the answer in one line plus the printf/cout line.
    :
    : Is the need for a separate function just a part of this class structure at this point in its schedule or is it more proper to perform everything in separate functions? Would I be a problem student in this class?:-)
    :
    : Comments please. Thanks.
    :
    : Take Care,
    : Ed

    [green]
    Rule of thumb, if you use it more than once then write a function, otherwise, it would not be needed. Sometimes though macros are better to use than functions for speed purposes.
    [/green]

  • stoberstober Posts: 9,765Member ✭✭✭
    I think you need to remember that what they do in academic world does not always reflect real-world problems. Under those conditions, it seems proper to write a one-line function just to teach someone how functions work. Writing a huge function at that point in their education might be too overwhelming.

    As a general rule, make one-line functions inline so that they look like functions but really aren't. Inline functions are most common in c++ programs.
  • Ed HallEd Hall Posts: 395Member
    : As a general rule, make one-line functions inline so that they look like functions but really aren't. Inline functions are most common in c++ programs.
    :

    Ah ha, I've never used an inline function although I remember reading about them in some tutorials. Thanks!

    Off on a further study. . .

    Take Care,
    Ed

  • Ed HallEd Hall Posts: 395Member
    [b][red]This message was edited by Ed Hall at 2004-11-10 12:50:49[/red][/b][hr]
    Although I'm probably doing someone's homework (if they happen to read this thread), I wrote a quick program with three methods of determining coin flips. Please correct anything I have incorrectly done or seem to be confused with. This was built to compile in Dev-C++ as a console program. I've tried to incorporate a function call flip(), an inline function call flip2() and my functionless version. Do note that in these respects I'm not referring to rand() as a funtion while it technically is one. I'm referring to functions I've written into the below code.

    [code]
    #include
    #include

    using namespace std;

    int flip()
    {
    return rand()%2;
    }

    inline int flip2() {return rand()%2;}

    int main()
    {
    int h, passes=100;
    srand(time(0));

    [blue]// this section includes a call to function flip
    h=0;
    for (int i=0;i0 ? 1: 0;
    printf("Of %d flips, Heads showed up %d times and Tails %d.

    ", passes, h, passes-h);[/blue]

    [green]// this section includes a call to inline function flip2
    h=0;
    for (int i=0;i0 ? 1: 0;
    printf("Of %d flips, Heads showed up %d times and Tails %d.

    ", passes, h, passes-h);[/green]

    [purple]// this section is self-dependent
    h=0;
    for (int i=0;i0 ? 1: 0;
    printf("Of %d flips, Heads showed up %d times and Tails %d.

    ", passes, h, passes-h);[/purple]


    system("PAUSE");
    return 0;
    }
    [/code]

    Would the third version be less or more appropriate, or is it a programmer's style issue?

    All comments welcomed. Thanks!

    Take Care,
    Ed



  • dennisparkerdennisparker Posts: 478Member
    [b][red]This message was edited by dennisparker at 2004-11-10 13:6:18[/red][/b][hr]
    : Please forgive me if my lack of formal programming education is the real trouble, but I see problems being asked on the board now and then where part of the problem states to call a function to do something that I can do as part of one line of code.
    :
    : If one line of code can be used within main() to determine some result, would it still be "better" or perhaps more "proper" to send out to a function?
    :
    : As an example, the recent post "coin flip in C++" states "the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads."
    :
    : If I write this program with a function called flip, I have to add several lines. If I omit the need for the function flip, I can calculate the answer in one line plus the printf/cout line.
    :
    : Is the need for a separate function just a part of this class structure at this point in its schedule or is it more proper to perform everything in separate functions? Would I be a problem student in this class?:-)
    :
    : Comments please. Thanks.
    :
    : Take Care,
    : Ed
    :
    :

    I have found it important to seperate into functions those parts of a program that perform an identifiable job.

    For instance, the line

    [code]
    SetDigital( INDEX, ON );
    [/code]

    performed the same task as

    [code]
    Extend_Index();
    [/code]

    Which was simply a two line function:

    [code]
    int Extend_Index( void ){
    SetDigital( INDEX, ON );
    return TRUE;
    }
    [/code]


    in a recent project. The reason it was useful to put that "job" into a function became apparent when unforseen modifications to the machine changed how the job was performed.

    The new code:

    [code]
    int Extend_Index( void ){
    int return_value;
    SetDigital( INDEX, ON );
    MilliDelay( 2000 );
    if( GetDigital(PROX, &bValue) )
    return_value = TRUE;
    else
    return_value = FALSE;
    }
    [/code]

    took care of the mods. I only had to change the code within Extend_Index() rather than dozens of other places in main(). This is the reason that I prefer to put most of my code, even one liners, into functions.






  • shaolin007shaolin007 Posts: 1,018Member
    : [b][red]This message was edited by Ed Hall at 2004-11-10 12:50:49[/red][/b][hr]
    : Although I'm probably doing someone's homework (if they happen to read this thread), I wrote a quick program with three methods of determining coin flips. Please correct anything I have incorrectly done or seem to be confused with. This was built to compile in Dev-C++ as a console program. I've tried to incorporate a function call flip(), an inline function call flip2() and my functionless version. Do note that in these respects I'm not referring to rand() as a funtion while it technically is one. I'm referring to functions I've written into the below code.
    :
    : [code]
    : #include
    : #include
    :
    : using namespace std;
    :
    : int flip()
    : {
    : return rand()%2;
    : }
    :
    : inline int flip2() {return rand()%2;}
    :
    : int main()
    : {
    : int h, passes=100;
    : srand(time(0));
    :
    : [blue]// this section includes a call to function flip
    : h=0;
    : for (int i=0;i0 ? 1: 0;
    : printf("Of %d flips, Heads showed up %d times and Tails %d.

    ", passes, h, passes-h);[/blue]
    :
    : [green]// this section includes a call to inline function flip2
    : h=0;
    : for (int i=0;i0 ? 1: 0;
    : printf("Of %d flips, Heads showed up %d times and Tails %d.

    ", passes, h, passes-h);[/green]
    :
    : [purple]// this section is self-dependent
    : h=0;
    : for (int i=0;i0 ? 1: 0;
    : printf("Of %d flips, Heads showed up %d times and Tails %d.

    ", passes, h, passes-h);[/purple]
    :
    :
    : system("PAUSE");
    : return 0;
    : }
    : [/code]
    :
    : Would the third version be less or more appropriate, or is it a programmer's style issue?
    :
    : All comments welcomed. Thanks!
    :
    : Take Care,
    : Ed

    [green]
    In my opinion the 3rd would be more efficient since it has less calls to other routines that use a nested subroutine but then again if you are working on a big project and want to modify your code with the least possible pains then it would be advisable to write a function specifically to handle that so that modification would be trivial.
    [/green]

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [blue]Bravo! Nice example of good program maintainability.
    The key is logical separation of a project into functions - if some piece of code has a logical conclusion - there should be a function with the name describing that logic (no matter if the code repeated even once - in the future - the same functionality can be required in other part(s) of the code, so if you already have the function - program becomes easily readable/maintainable).[/blue]

    : [b][red]This message was edited by dennisparker at 2004-11-10 13:6:18[/red][/b][hr]
    : : Please forgive me if my lack of formal programming education is the real trouble, but I see problems being asked on the board now and then where part of the problem states to call a function to do something that I can do as part of one line of code.
    : :
    : : If one line of code can be used within main() to determine some result, would it still be "better" or perhaps more "proper" to send out to a function?
    : :
    : : As an example, the recent post "coin flip in C++" states "the program should call a separate function flip that takes no arguments and returns 0 for tails and 1 for heads."
    : :
    : : If I write this program with a function called flip, I have to add several lines. If I omit the need for the function flip, I can calculate the answer in one line plus the printf/cout line.
    : :
    : : Is the need for a separate function just a part of this class structure at this point in its schedule or is it more proper to perform everything in separate functions? Would I be a problem student in this class?:-)
    : :
    : : Comments please. Thanks.
    : :
    : : Take Care,
    : : Ed
    : :
    : :
    :
    : I have found it important to seperate into functions those parts of a program that perform an identifiable job.
    :
    : For instance, the line
    :
    : [code]
    : SetDigital( INDEX, ON );
    : [/code]
    :
    : performed the same task as
    :
    : [code]
    : Extend_Index();
    : [/code]
    :
    : Which was simply a two line function:
    :
    : [code]
    : int Extend_Index( void ){
    : SetDigital( INDEX, ON );
    : return TRUE;
    : }
    : [/code]
    :
    :
    : in a recent project. The reason it was useful to put that "job" into a function became apparent when unforseen modifications to the machine changed how the job was performed.
    :
    : The new code:
    :
    : [code]
    : int Extend_Index( void ){
    : int return_value;
    : SetDigital( INDEX, ON );
    : MilliDelay( 2000 );
    : if( GetDigital(PROX, &bValue) )
    : return_value = TRUE;
    : else
    : return_value = FALSE;
    : }
    : [/code]
    :
    : took care of the mods. I only had to change the code within Extend_Index() rather than dozens of other places in main(). This is the reason that I prefer to put most of my code, even one liners, into functions.
    :
    :
    :
    :
    :
    :
    :

Sign In or Register to comment.