ADTs and Black Boxes Functions in C

This C Tutorial Explains ADTs and Black Boxes in Context with Functions in C.

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.

advertisement
advertisement

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,

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.