Being Logical with functions in C++

Who, what, when, where and why for functions.

Page 1 of 1

4 Replies - 1143 Views - Last Post: 08 November 2009 - 11:14 AM Rate Topic: -----

#1 Brink Kale   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 09

Being Logical with functions in C++

Posted 08 November 2009 - 07:47 AM

I have been trudging through a number of C++ tutorials and while still at the basics, I learned about functions not too long ago and seem to be running into issue with them now. It isn't so much an issue with the code but rather with my thinking. It seems that I have been trying to make everything and its dog a function. I was wondering what exactly the experts think in regards to functions in C++. When is the best time to use a function while coding. Is it possible to be "too function happy"? To be frank, I have been extremely function happy, making pretty much any and everything a function and calling it in my main (in sequence).

Is This A Good Question/Topic? 0
  • +

Replies To: Being Logical with functions in C++

#2 redhotfire0   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Being Logical with functions in C++

Posted 08 November 2009 - 08:13 AM

You can never have to many functions, it just how they are used that might make it hard or confusing. I myself make everything a function but only call one function instead:

#include <stdio.h>

void anotherFunction(void)
{
 printf("\nHello again!");
}

void FunctionForAll(void)
{
 printf("Hello.");
 anotherFunction();
}


int main(void)
{
 FunctionForAll();
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#3 Brink Kale   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 06-November 09

Re: Being Logical with functions in C++

Posted 08 November 2009 - 08:30 AM

A function for calling my functions, the thought never crossed my mind. I was getting particularly worried about this because of two reasons. My code was beginning to overwhelm me a bit and I have read a surplus of tutorials that have most code within the main and only "necessary" functions made to do things. It dawned on me while trying to do a simple square for a simple math equation I set the program to preform that instead of creating an entire function, I could just make a variable and set it equal to the number's variable multiplied by itself. I then smacked myself on the head and began wondering if maybe I was going a bit "overboard" on the functions at that point.
Was This Post Helpful? 0
  • +
  • -

#4 redhotfire0   User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 210
  • Joined: 13-July 09

Re: Being Logical with functions in C++

Posted 08 November 2009 - 08:55 AM

View PostBrink Kale, on 8 Nov, 2009 - 07:30 AM, said:

A function for calling my functions, the thought never crossed my mind. I was getting particularly worried about this because of two reasons. My code was beginning to overwhelm me a bit and I have read a surplus of tutorials that have most code within the main and only "necessary" functions made to do things. It dawned on me while trying to do a simple square for a simple math equation I set the program to preform that instead of creating an entire function, I could just make a variable and set it equal to the number's variable multiplied by itself. I then smacked myself on the head and began wondering if maybe I was going a bit "overboard" on the functions at that point.

Not over board, just not thinking about using already made functions made available to you. For an example for your example, you could use pow() in math.h to multiple a number by itself or as you tried to do, prototype a function that will do something similar.
Was This Post Helpful? 0
  • +
  • -

#5 Bench   User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

Re: Being Logical with functions in C++

Posted 08 November 2009 - 11:14 AM

To my mind, functions are rarely a bad thing; Though the important idea to recognise is that functions are one of many tools at your disposal which allow you to write better code (e.g. more flexible, more readable, more maintainable, more reusable etc); though they don't prevent you from writing bad code.

If you reallly believe that a function in your program is completely superfluous then maybe it is. Your own judgement (Or that of a friend/colleague) is usually the best way to determine whether a function is good or whether its unnecessary.. As you gain more experience in tackling larger problems, you'll find that this decision becomes more like second nature.

Off the top of my head, here are a few situations I can think of where I might turn a block of code into a function -
  • A Block of code which is near identical to some other block of code (Avoiding 'copy and paste' type repetition)
  • A Block of code which can be described as a simple operation; e.g, 'sort', 'swap', 'search', etc.
  • A repeated or conditional block of code where the condition or number of repetitions is based upon some user input or persistent data (And thus that same block may be replicated elsewhere at some later date)
  • A repeated or conditional block of code which might otherwise be nested within another repeated or conditional block (Functions are useful for avoiding large amounts of indentation with 'nested' code)
  • To break up a very large block of code which spills beyond one screen in your IDE or text editor.
  • Empty functions used as a placeholder for future enhancements, or just to describe a step in a routine which hasn't yet been fully implemented.
I'm sure there are plenty more situations - note that I have been deliberately vague in the length of functions; sometimes a good function may only be a single statement, but if the name of that function adds additional context or meaning to that statement, then sometimes wrapping it up can be very good for readability, and reduce the amount of commenting needed to explain what your code is supposed to do. (Self documenting code).

Final thought on functions - be thoughtful with names; they can make all the difference between code which is easy to read vs cryptic code requiring a load of guesswork. Wherever you can, include strong verbs which describe its behaviour; eg start_widget(), find_highest(), grab_next_item(), etc.

This post has been edited by Bench: 08 November 2009 - 11:16 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1