top of page

How Functions... Function in C

Introduction


Harvard Professor David Malan compares an algorithm to a black box in which some input is processed and some output is returned.


In the same way, functions also befit this metaphor.



The function acts as a "processor", manipulating the input (if any) and outputting a single value. This guide will break down functions, from their uses to their implementation!


Why Use a Function?


A common theme throughout programming is keeping with correctness, style, good design. Functions help us mainly with the latter two in these ways:


  • Organization

    • helps to decompose a larger problem into smaller parts

    • easily scannable when going over code

      • ex: one aptly-named line like add(1, 2); tells us what that part of the program is doing

  • Simplification

    • breaking a larger problem into smaller subparts tends to make the program easier to implement and debug

  • Reduces Redundancy

    • One function can be used many, many times

    • Instead of repeating code, you can simply write a single line of code


Writing a Function


Your function will start with a header...

return-type name(arguments) 

Then curly braces...

return-type name(arguments) 
{
	...
}

Then the contents and return. For example, a function to add two numbers could look like this:

int add(int a, int b)
{
	return a + b;
}

Note that you can do anything within a function, including creating and manipulating variables:

int add(int a, int b)
{
	int sum = a + b;
	return sum;
}

Just make sure that the value you return is of the same type specified in the header!


Declaring a Function


Because the C compiler scans code from top to bottom, it's important, for both convention and correctness, that you "declare" each function atop the main() method.


Basically, these declarations or function prototypes give the compiler a heads-up that a function will appear later on in the program.


A function declaration or prototype will look like pretty much identical to its header:

return-type name(arguments);

*arguments is just another name for inputs in their variable form (like int a, int b below)


For instance:

int add(int a, int b);

Another, more concrete example:



Technically, you could get away with writing your functions above main() (where they will likely be called) without having to declare them because the compiler has registered the function before it is called, unlike in the previous image where the function is implemented after it is called:



But this is considered against the convention, as when programs grow larger in scale and complexity, it can be useful to be able to scan the top of the file to see all available functions.


Calling a Function


Calling a function just means evoking the functions you wrote. Again, it's very similar to the function header, but replacing the return types with actual values:

name(parameters);

*parameters is just another name for input / arguments, but in their actual value form (like 1 and 2) instead of (a and b)



Here, the function multiply() is called on 100 and 24, storing the value 2400 in the variable product. Notice how we can reuse this function with many different numbers, making it extremely versatile.


Final Thoughts


One last thing you might be wondering: Why does main not return anything when its return type is specified as an int?


Interestingly, this particular function automatically returns a 0 (denoting success) unless you as the programmer explicitly return another int value.


Anyways, I hope this overview of functions in C was useful. Thanks for reading!

Comments


bottom of page