What are two main components to using functions (there are three from you to choose from)? What do each do? Why are they important?

Answers

Answer 1

Answer:

function is a self-contained program segment that carries out some specific, well-defined task. Every C program consists of one or more functions. One of these functions must be called main. Execution of the program always begins by carrying out the instructions contained in main. Note that if a program contains multiple functions then their definitions may appear in any order. The same function can be accessed from several different places within a program. Once the function has carried out its intended action, control is returned to the point from which the function was accessed. Generally speaking, a function processes information passed to it from the calling portion of the program, and returns a single value. Some functions, however, accept information but do not return anything.

A function definition has two principal components: the first line (including the argument declarations), and the so-called body of the function.

The first line of a function takes the general form data-type  name(type 1  arg 1,  type 2  arg 2,  ...,  type n  arg n)

where data-type represents the data type of the item that is returned by the function, name represents the name of the function, and type 1, type 2, ..., type n represent the data types of the arguments arg 1, arg 2, ..., arg n. The allowable data types for a function are:

int       for a function which returns an integer value

double    for a function which returns an floating-point value

void      for a function which does not return any value

The allowable data types for a function's arguments are int and double. Note that the identifiers used to reference the arguments of a function are local, in the sense that they are not recognized outside of the function. Thus, the argument names in a function definition need not be the same as those used in the segments of the program from which the function was called. However, the corresponding data types of the arguments must always match.

The body of a function is a compound statement that defines the action to be taken by the function. Like a regular compound statement, the body can contain expression statements, control statements, other compound statements, etc. The body can even access other functions. In fact, it can even access itself--this process is known as recursion. In addition, however, the body must include one or more return statements in order to return a value to the calling portion of the program.

A return statement causes the program logic to return to the point in the program from which the function was accessed. The general form of a return statement is:

return  expression;

This statement causes the value of expression to be returned to the calling part of the program. Of course, the data type of expression should match the declared data type of the function. For a void function, which does not return any value, the appropriate return statement is simply:

return;

A maximum of one expression can be included in a return statement. Thus, a function can return a maximum of one value to the calling part of the program. However, a function definition can include multiple return statements, each containing a different expression, which are conditionally executed, depending on the program logic.

Note that, by convention, the main function is of type int and returns the integer value 0 to the operating system, indicating the error-free termination of the program. In its simplest form, the main function possesses no arguments. The library function call exit(1), employed in previous example programs, causes the execution of a program to abort, returning the integer value 1 to the operating system, which (by convention) indicates that the program terminated with an error status.

The program segment listed below shows how the previous program factorial.c can be converted into a function factorial(n) which returns the factorial (in the form of a floating-point number) of the non-negative integer n:

double factorial(int n)

{

 /*

    Function to evaluate factorial (in floating-point form)

    of non-negative integer n.

 */

 int count;

 double fact = 1.;

 /* Abort if n is negative integer */

 if (n < 0)

  {

   printf("\nError: factorial of negative integer not defined\n");

   exit(1);

  }

 /* Calculate factorial */

 for (count = n; count > 0; --count) fact *= (double) count;

 /* Return value of factorial */

 return fact;      

}

Explanation:


Related Questions

Other Questions
Which of the following would be an effect of a hurricane on an ecosystem?O A forest fire burns the plantlife close to the ground.O The salt concentration in an area of water increases due to an influx of seawater.O Plant life dies due to a lack of available water.O A hard freeze kills the local plantlife in a forest ecosystem. Ivan included this sentence in a story he wrote about his camping trip:While my mom and I were setting up the tent, my father started the campfireWhat two verb tenses did Ivan use in his sentence?A. Past progressive and simple past.B. Present progressive and simple past.C. Present progressive and simple present.D. Future progressive and simple present.Please use correct answers I'm trying to get good grades!!!!!I'm paying generous amount of points !!!!! Emma's sister is nine years olderthan Emma. Their combined agesadd up to 49. How old is Emma?(The answer isnt 40 bc it say that her sister is 9 years OLDER then Emma) pls help Please help me out.Can the lengths 40, 60, and 80 form a supplementary triangle? 7. Which statement below is accurate about e-cigarettes? Read about the biotechnology R&D work profile described and fill in the blank robin works in a laboratory at a large research hospital. The _____ she studies are too tiny to see with a microscope there are 4 white marbles 6 red marbles and 2 blue marbles and 4 green marbles if the marble is chosen randomly what is the probability it is not green Write a polynomial function f of least degree that has rational coefficients, a leading coefficient of 1, and the given zeros. Write thepolynomial in standard form.3i, 2 - i $1500 at 9% for 2 years 2. Which of the following is not a characteristicof a claim?As logicaB. It should be interesting and engagingC. It should be specific and notfocusedD. It should be argumentative anddebatable I my last questions got reported so...here are 100pointswhat is [tex]107 \times 58[/tex] he hurt his leg,so he (not walk) very well. what is the coefficient of -2x+8=10 Carmen has 240 action figures she wants to display in 4 bookcases. Each bookcase has 5 shelves, and each shelf gets the same number of action figures. What is true about how Carmen can display her action figures?She can put ____ action figures in each bookcase She can put ____ action figures on each shelfThe choices area. 5b. 12c. 30d. 60e. 231f. 249PLESSE HELP Find the closest point on the line. y = - x -4 to (4,4) This purpose of joint operations is to maintain an arsenal capable of deterring potential adversaries and to assure U.S. allies and other security partners that they can count on America's security commitments.a. Trueb. False What is more important, environmental preservation or acquisition of earths materials? | ~ ( Back up your response with actual scientific facts. ) ~ | Please select the word from the list that best fits the definitionsite of the conference where Hitler promised not to expand Germanys territoryMunichNeville chamberlainWinston ChurchillPearl harborNanjing We're no strangers to loveYou know the rules and so do IA full commitment's what I'm thinking ofYou wouldn't get this from any other guyI just wanna tell you how I'm feelingGotta make you understandNever gonna give you up, never gonna let you downNever gonna run around and desert youNever gonna make you cry, never gonna say goodbyeNever gonna tell a lie and hurt youWe've known each other for so longYour heart's been aching but you're too shy to say itInside we both know what's been going onWe know the game and we're gonna play itAnd if you ask me how I'm feelingDon't tell me you're too blind to seeNever gonna give you up, never gonna let you downNever gonna run around and desert youNever gonna make you cry, never gonna say goodbyeNever gonna tell a lie and hurt youNever gonna give you up, never gonna let you downNever gonna run around and desert youNever gonna make you cry, never gonna say goodbyeNever gonna tell a lie and hurt you(Ooh, give you up)(Ooh, give you up)(Ooh) never gonna give, never gonna give (give you up)(Ooh) never gonna give, never gonna give (give you up)We've known each other for so longYour heart's been aching but you're too shy to say itInside we both know what's been going onWe know the game and we're gonna play itI just wanna tell you how I'm feelingGotta make you understandNever gonna give you up, never gonna let you downNever gonna run around and desert youNever gonna make you cry, never gonna say goodbyeNever gonna tell a lie and hurt youNever gonna give you up, never gonna let you downNever gonna run around and desert youNever gonna make you cry, never gonna say goodbyeNever gonna tell a lie and hurt youNever gonna give you up, never gonna let you downNever gonna run around and desert youNever gonna make you cry, never gonna say goodbyeNever gonna tell a lie and hurt you Which example is most likely made from both metals and gemstones?