Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

need answers for No4 and No5 other can be thought of as miniature of other progr

ID: 3809983 • Letter: N

Question

   need answers for No4 and No5

other can be thought of as miniature of other programs. Like any program, we can think of functions as having input and output to communicate with the main program. (a) How does a program provide "input" to one of its functions? (b) How does a function provide "output" to the program? 5. Consider this very simple function: def cube (x) answer X X return answer (a) What does this function do? (b) show how a program could use this function to print the value of va, assuming y is a variable. (c) Here is a fragment of a program that uses this function: result a cube (3) print (answer result) The output from this fragment is 4 27. Explain why the output is not 27 27, even though cube seems to change the value of answer to 27. Programming Exercises 1. Write a program to print the lyrics of the song "old MacDonald." Your program should print the lyrics for five different animals, similar to the example verse below. old MacDonald had a farm, Ee-igh, Ee-igh, oh! And on that farm he had a cow, Ee-igh, Ee-igh, oh! With a moo, moo here and a mao, moo there.

Explanation / Answer

4)a. A function takes the inputs to the program called as arguments.we can either send the parameter through "call by value" or "call by reference"

eg: int main() {

int a,b;

printf("enter 2 numbers:");

scanf("%d %d",&a,&b);

printf("sum of 2 numbers is:");

add(a,b);

}

add(x,y) {

x=a;

y=b;

return (a+b);

}

here if the inputs are given as 5,6 then the add(5,6) is called sub program to the main function

b.for output the program uses the return or output function after the function is called. in the above example return is used to output the value so obtained after calling the function add(a,b) the value is returned to main or any other sub programs if called.

5.a)The given function finds the cube of a number given as input and returns the answer4

5.b) doublr cube(double y){

    return (y* y* y);

}

int main()

{

int y;

double c;

printf("Enter any number: ");

scanf("%d", &y);

c = cube(y);

printf("Cube of %d is %.2f ", y, c);

return 0;

}

so when the number is given as 2 then the output is 8 since 2*2*2 is 8.

5.c)since answer is a variable and already assigned a value 4 and did not call a function when print(answer,result) is 4,27 only not 27,27

  

5.b) doublr cube(double y){

    return (y* y* y);

}

int main()

{

int y;

double c;

printf("Enter any number: ");

scanf("%d", &y);

c = cube(y);

printf("Cube of %d is %.2f ", y, c);

return 0;

}

so when the number is given as 2 then the output is 8 since 2*2*2 is 8.

5.c)since answer is a variable and already assigned a value 4 and did not call a function when print(answer,result) is 4,27 only not 27,27