Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Ques write a program in the language c which meets all the requirements below. 1. Function definitions, declarations and header files Guide C is a

Ques write a program in the language c which meets all the requirements below.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

1. Function definitions, declarations and header files Guide C is a structured, procedural programming language, that is, it supports the definition of isolated, self-contained blocks of code that can be re-used as components of larger programs. In C and Python these are called functions, but other languages call them procedures, subroutines, or methods. Given a well-designed function, you can often use it for what it does, and ignore the internal details of how it works. This idea of functional encapsulation makes it feasible for humans to write complex programs by breaking them down into small, more manageable pieces. You have already used several functions provided by the standard library, such as printf and floor, without seeing how they were implemented. One such function is max ( ), which could be implemented and used as follows 1. // returns the larger of the two arguments 2. int max( int x, int y) 4. int larger 5, if( x > y ) Larger X 9. return larger; 10.h 12. int main( void) 13. 14. int a-11; 15. int b = 12; 16. printf( "The max of our numbers is %d ", max ( a, b ); 17. return 0 18. h Reading the first 9 lines of code top-to-bottom, left-to-right, we see Line 1: A comment describing what the following function does. Commenting our code is a good programming style to adopt Lines 2-10 are the function definition, as follows: . The result, or return value of running this function is of type int . The name of the function is max . The function takes two integer arguments called x and y The body of the function, i.e. the code that will execute when the function is called, starts at ( In the body of the function, the keyword return evaluates the expression to it's right which becomes the function's return value, then exits the function Creating functions with only one return statement (only one exit point) is a good programming style to adopt. The body of the function ends at the closing ) . . Having been defined in lines 2 to 10, function max() is called on line 16. The values of integers a and b are assigned to x and y inside max (), the function does its work, and the return value is passed as the second argument to printf() The syntax for C functions is type functionName type arg_namel [, type arg_name2, ...) // code-block function body enclosed by curly braces Note that main() itself is a normal function. The system calls it to start the program The name "function" is a little unfortunate, since these are not the same as mathematical functions. Mathematical functions express relationships, while program functions perform sequences of operations on data. To see the difference, consider the function max() above and the following function output() 1. void output ( double val ) 3. printf( "the value is %.2f ", val ); where void is a special type that means "nothing". Since there is nothing to return, we can omit the return statement and the function will finish at the end of the code block. Clearly output) does not describe a relationship between entities; instead it causes useful things to happen. Strictly speaking this is true of the C function max () as wel: it implements the mathematical function max() by returning the largest of its arguments We are stuck with the C convention of calling our procedures "functions". In practice this is not usually a problem. We will return to the differences between "true" functions and C functions later in a discussion of software engineering approaches Multiple arguments As the syntax above indicates, function definitions can specify an arbitrary number of arguments, for example 1. double volumedouble width, double height, double depth ) 3. return width*height*depth; However, C functions can have only a single return value. Other languages such as Lisp and Go support multiple return values, so this asymmetry between the number of arguments and results was a design decision in C. It keeps the syntax simple Functions in functions Since main) is a function, it should be apparent that functions can call other functions. In general a function may call any other function. One interesting situation is when a function calls itself, a process called recursion. Many algorithms are elegantly expressed as recursive functions. You may have seen this idea before proof by induction relies on it. Function Declarations Above we looked at three function definitions: max (), output( and volume (). A function can be called in a C source code file below its definition. Why only below? The compiler reads the source file from top to bottom. If it sees a call to function foo(), it doesn't know anything about that function. Did you specify the right number and type of arguments? There's no way to know. The compiler will complain When you want to use a function that is not defined in your source file, for example a standard library function, we need some way to tell the compiler the name of the function, its arguments and return type. The implementation of the function will be provided from a library or some other source file A function declaration is the same as its definition, but with the function body replaced by a semicolon. Thus for volume() above, a matching declaration is double volume( double width, double height, double depth); Header files Your code almost always begins by including some header files #include #include (etc) Recall that #include copies the contents of the indicated file into the current source file. Specifying the name of the header file forces the compiler to look for the file in the directories where your compiler was installed. To specify file you wrote yourself put the filename in quotes "like this". Foir example #include // a system-supplied header #include "myheader.h" // a header I wrote myself The quotes version understands paths relative to the current directory, and absolute paths, e.g #include "myheader.h" // in the current directory #include "include/myheader.n" // in a subdirectory #include "../myheader.h" // in the parent directory #include "/opt/local/include/myheader.h.' // an absolute path C header files usually contain mostly function declarations. In the case of the standard library the implementations of these functions were pre-compiled for you into code libraries and shipped with your compiler to save you time. The required libraries are linked into your program at the last step of compilation. The library containing printf () is used so often it is linked by default. The math library containing floor (), etc. is used less often, so we have to request it in in the compile command. It is still used a lot, so the designer gave it the nice short name m. $ gcc mycode.c -1m Header files also provide important constant values, such as the special value EOF (usually -1) returned by scanf ) when reaching the end of a file Occasionally they also declare global variables. Now you know how to define and declare functions, and what header files do. Using multiple source files Function declarations and header files are also used when breaking your own code up into multiple source files. For example, in your working directory you have the source files funcs.c containing useful function definitions and main.c containing your main program Contents of funcs.c: 1. // returns the larger of the two arguments 2. int max( int x, int y) 4. int larger=y; 5. if(x> y largerx; 9. return larger; 10. ) 12. // returns the smaller of the two arguments 13. int min( int x, int y) 14. 1 15 16. if( x 3. int main void . int a = 11; 6. int b 12; 7. printf( "The max of our numbers is %d. ", max ( a, b )); 8. printf( "The min of our numbers is %d. ", min ( a, b )); 9. return 0 10. ) Given multiple source files on its command line, the compiler will attempt to compile them all into one program. Try this now: | s gcc -Wall funcs.c main.c The -Wal1 compiler option enables "all warnings". The compiler therefore will be very strict when it compiles your code and will warn you about potential problems With these files as they are you will get warnings and possibly errors complaining that in main.c, min() and max () are not properly declared. The exact message you get depends on your compiler vendor and version, but in CSIL we get main.c:7:45: warning: implicit declaration of function 'max' is invalid in C99 [-Wimplicit-function-declaration] printf( "The max of our numbers is %d ", max ( a, b )); main.c:8:45: warning: implicit declaration of function min' is invalid in C99 [-Wimplicit-function-declaration] printf( "The min of our numbers is %d ", min ( a, b )); 2 warnings generated Note that your code will execute even if it generated some warnings during compilation. However, it is good programming practice to investigate the cause(s) of the warnings as they may indicate potential problemsin your code Requirements First, check that you completed the Setup section above, so your current working directory is /2 . The shell command pwd prints the current working directory 1. Create a new file called funcs.h that contains function declarations for all the functions in funcs.c. Remember (as we have seen above) that a function declaration is the same as its definition, but with the function body replaced by a semicolon. Thus for volume) above, a matching declaration is double volume ( double width, double height, double depth; Edit the file main.c to #include the new header file 2. The finished code must compile with this command with no errors or warnings $ gcc main.c funcs.c 3. The finished code must output The max of our numbers is 12 The min of our numbers is 11. Testing and Submitting The compiler will produce informative complaints if your declarations are not correct. Read the compiler output very carefully, starting with the top lines Submit by adding your header file and committing your changes git add funcs.h git commit-m "taskl submit" git push

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions

Question

The models used to analyse different national cultures.

Answered: 1 week ago

Question

The nature of the issues associated with expatriate employment.

Answered: 1 week ago