Many programmers like to think of a function as a “black box” defined in terms of the information that goes in (its input) and the value or action it produces (its output). What goes on inside the black box is not your concern, unless you are the one who has to write the function. For example, when you use ‘printf()’, you know that you have to give it a control string and, perhaps, some arguments. You also know what output ‘printf()’ should produce. You never have to think about the programming that went into creating ‘printf()’. Thinking of functions in this manner helps you concentrate on the program’s overall design rather than the details. Think carefully about what the function should do and how it relates to the program as a whole before worrying about writing the code. Let’s see an example,
#include <stdio.h> int largerof2ints(int, int); int main(void) { int num1, num2, larger; printf("\nProgram displays larger of two integers.\n"); printf("User, enter two integers...,(q to quit)\n"); while (scanf("%d %d", &num1, &num2) == 2) { larger = largerof2ints(num1, num2); printf("larger of two integers %d and %d is %d\n", num1, num2, larger); printf("User wanna continue, enter two integers...,(q to quit)\n"); } return 0; } int largerof2ints(int n, int m) { return if (n > m) ? n : m; }
Output of program as follows:
Program displays larger of two integers. User, enter two integers...,(q to quit) 23 12 larger of two integers 23 and 12 is 23 User want to continue, enter two integers...,(q to quit) 67 1111 larger of two integers 67 and 1111 is 1111 User want to continue, enter two integers...,(q to quit) q
In order to implement black-box and abstract data-types in context with functions, practice to place function definitions in a separate file, say file2.c, and all function headers and named constants in a separate user defined header, for example, fun_header.h. Remember that user specified header should be enclosed in double quotes to indicate that it exists in the current directory.
Considering the above information, we rewrite the above program below as,
/* fun_blackbox2.c -- program implements black-box programming */ /* reference about user defined header */ #include "fun_header.h" int main(void) { int num1, num2, larger; printf("\nProgram displays larger of two integers.\n"); printf("User, enter two integers...,(q to quit)\n"); while (scanf("%d %d", &num1, &num2) == 2) { larger = largerof2ints(num1, num2); printf("larger of two integers %d and %d is %d\n", num1, num2, larger); printf("User wanna continue, enter two integers...,(q to quit)\n"); } return 0; } /* fun_def.c -- all function definitions are defined here */ int largerof2ints(int n, int m) { return if (n > m) ? n : m; } /* * fun_header.h -- all function declarations and symbolic constants are * declared in the header */ #include <stdio.h> int largerof2ints(int, int);
How to compile a multi-file program on Linux or Unix system? let’s see the command for the above program, for example,
gcc fun_blackbox2.c fun_def.c on Linux cc fun_blackbox2.c fun_def.c on Unix
Advantages of programming functions as black-boxes simplifies ‘main()’ function. ‘main()’ function includes only interfaces to calling functions, no prototypes, no defined constants and no function definitions. Further, functions can be shared across several other programs leading to reputability of functions.
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
- Practice BCA MCQs
- Apply for C Internship
- Check C Books
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs