User Tools

Site Tools



This shows you the differences between two versions of the page.

Link to this comparison view

writing_helper_functions [2019/04/08 20:11] (current)
Line 1: Line 1:
 +===== Writing Helper (internal) Functions =====
 +Helper functions are internal functions that we create when we break a programming problem into smaller pieces. Many times, it is our intention, as programmers,​ to only use these functions internally and to not allow their use outside the program that we are working on. The way that you approach this with C differs considerably from C++. Here is an example to illustrate how to implement helper functions. If you carefully read the comments, you should understand how to use internally-declared helper functions.
 +<code C>
 +#include <​stdio.h>​
 +#include <​stdint.h>​
 +#include <​math.h> ​ // Need sqrt().
 +// Note that the hypotenuse function is not declared
 +// in a .h file anywhere. The intent is that hypotenuse
 +// function will only be used inside main for this example.
 +// Function prototype for the helper function. Tells
 +// the compiler that this function will be defined
 +// later.
 +int32_t hypotenuse(int32_t a, int32_t b);
 +int main() {
 +  int32_t a = 10;
 +  int32_t b = 20;
 +  int32_t c;
 +  c = hypotenuse(a,​ b);
 +  printf("​Length of hypotenuse: %d\n\r",​ c);  ​
 +// Definition of the hypotenuse function.
 +int32_t hypotenuse(int32_t a, int32_t b) {
 +  return sqrt((a*a) + (b*b));
writing_helper_functions.txt ยท Last modified: 2019/04/08 20:11 (external edit)