Skip to content

Instantly share code, notes, and snippets.

@FreeER
Last active January 10, 2019 13:30
Show Gist options
  • Save FreeER/7e6a2228ca28775f1d766ef1f14e45c0 to your computer and use it in GitHub Desktop.
Save FreeER/7e6a2228ca28775f1d766ef1f14e45c0 to your computer and use it in GitHub Desktop.
Comprehensive explanation of functions in C

can you guys recommend a good, comprehensive explanation of functions that goes beyond writing a function that adds two integers? More examples would be nice.

A function is a way to name a piece of code and reuse it with different values. The most basic example is something like... ah... I can't think of any examples off the top of my head that don't take or return anything right now but let's say closing the program and pretend it doesn't take anything. It might be a lot of code and you don't really care how it works just what it does eg. exit(); As an example of more than just a simple name imagine calculating the volume of a rectangular box:

int width = 53;
int height = 24;
int length = 32;
int volume = width * height * length;

While fairly simple already if it was something more complicated you might want to give it a name so that you/others can just read what its doing rather than have to figure it out from the individual lines of code eg.

// temporary globals for accessibility in example
int width = 53;
int height = 24;
int length = 32;
int volume = 0;
func calcVolume { // not valid syntax, equivalent would be: void calcVolume() { ... }
    volume = width * height * length;
}
// any time you want to recalculate the volume
calcVolume; // equiv. syntax: calcVolume();

Except, if you want to do it for multiple boxes now you have to write code like

width = 33;
height = 32;
length = 21;
calcVolume;
int localVolume = volume;

width = 13; height = 6; length = 8; // if you don't mind
calcVolume;
int localVolume2 = volume;

Which works, but is kind of ugly and is going to cause issues with multiple threads calling it at the same time because of the globals use and requires separate documentation for the user to read the code to even know which globals are used. So, there's a way to specify that it takes "arguments"/"parameters" (technically I think the latter applies to the actual values passed when it's called but the terms are used interchangeably by most) as well as what it returns: returnType functioName(list of type:name pairs); eg. ```c

// globals for example continuity
int width = 53;
int height = 24;
int length = 32;

int calcVolume(int width, int height, int length) {
    return width * height * length;
}

// any time you want to recalculate the volume
int avolume = calcVolume(width,height,length); // can use globals but values are copied into local variables
int anotherVolume = calcVolume(35,42,23); // can also pass values directly now, useful!

note: void is used as the return type when the function is not going to return anything.

Now, there is a slight issue with being able to change a variable inside a function. Before you used global variables which the function could change but now the value is copied into a local variable that, while changeable, other code can not see. So you'd have to return any values, and you'd need a struct to return multiple, and hope the user remembers to use it properly. While often valid you might potentially want the function to do the work itself. For example, when implementing a dynamic array structure where you want to be sure the stored size and pointer are updated. Which is where pointers come in, since you can pass (a copy of) the address to the function and then it can go to that address and change whatever value is there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment