Whats the best way to understanding Functions?

Hi, I was reading over our class lecture on functions and I don't seem to understand fully of what functions can do exactly and how to place them within a source code. Could anyone provide any helpful links or tips when using functions in a source code for example when do i use the data type void? only when I am displaying something and when I am storing I should use an appropriate data type?

Thanks in advance!

If I am correct, you want to know how we should decide return type of function?

as you said above when we use void...

I am assuming you are asking about return type, is it correct?
I typically use void functions when i just want to display something on the screen (player's health etc...) and i think that's there only purpose to be honest, is to just print certain crap on the screen since they return no actual values.
Hi,

your function contains code that you need often (if don't want to copy paste it each time, it's convenient to put it into a function)

... like the math functions ... 'tangens' f. ex.

You program now calls a function, but the returned value will be stored in previously allocated memory by your application

1
2
3
4
5
6
7
8

double a = 0;

...
...

 a = tan(x);


However, by "knowing" the return type, the compiler can check, wether there is enough memory, and if the return value will be interpreted in the right ( say "intended" ) way.

1
2
3
4
5
6
7
8

int a = 0;

...
...

 a = tan(x);


would result in a warning, because the return-value will be read as an "int", which is not what "tan" would usually return.

It might as well give erros, if return - types are complicated and can not be recasted so easily, as is the case with structs and so on.

However, thats the reason, why return types are nessecary, as far as I know.

Void as return type just means : "the function doesn't return anything".

Greets,

fluppe
Last edited on
Functions are a way to break your code into smaller chunks, so that you can repeat tasks without pasting the code over and over.

they are handy when breaking down a problem into smaller problems. each "sub" problem can be coded into a function and then called from another "sub" problem.

functions that dont return a result (ones that dont give you back an answer) are usually known as procedures, but in C/C++ they are just called functions and they have the return type "void"

eg;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void get_ready()
{
  // empty demo func
}


void get_set()
{
  // empty demo func
}

float do_race()
{
     float fastest_time;
     return fastest_time;
}

void my_main_func()
{
       get_ready();
       get_set();
       float fastest_time = do_race();
}

The easyest way to think of functions is they are
just code you can call at any time

e.g
1
2
3
4
5
6
7
8
9
10
11
12
#include <cstdio.h>

void hello()
{
 printf("Hello!");
}

int main()
{
 hello();//prints hello on screen
 return 0;
}

functions get more complicated than this but to start to understand them
this is a good way

also i am using printf as i am a c programmer not so much c++
but functions in c and c++ are practialy identical
Last edited on
Topic archived. No new replies allowed.