Lab08 Functions With Output Arguments

download Lab08 Functions With Output Arguments

of 3

Transcript of Lab08 Functions With Output Arguments

  • 8/11/2019 Lab08 Functions With Output Arguments

    1/3

    ICS 103: Computer Programming in C

    Lab #7: Functions with Output Parameters & ecursi!e Functions

    Objective:

    Learn how to write functions that return more than one result. Learn how to write recursive functions.

    Functions with output arguments

    From the previous lab, we know how to write a function that receives arguments (input arguments)and returns a single result using the return statement. There are many situations where we wouldlike a function to return more than one result. Here are some e amples!

    Function to convert time in seconds into hours, minutes and seconds Function to find the "uotient and remainder of a division Function to return ma imum, minimum and average from a set of values

    The return statement cannot be used to return more than one value, i.e. we cannot have thefollowing statement!

    return value#, value$%&lso, we cannot have two return statements following each other as!

    return value#%return value$%

    The solution in these cases is to make the return type of the function void i.e. the function does notuse the return statement to return the results . 'nstead, the results will be returned through thearguments ( output arguments ). 'n contrast to input arguments which are normal variables, outputarguments must be pointer variables . hen pointer variables are used as arguments, they allow thecalled function to access the variables created in the calling function. Thus, the results can be storeddirectly in these variables while the e ecution is in the called function.The following e ample shows how functions with output arguments work.

    #include #define PI 3.14

    void area_circum (double radius, double *area, double *circum !

    int main (void

    " double radius, a, c! rintf ($%nter the radius of the circle& $ !scanf ($'lf$, radius !

    area_circum (radius, a, c ! )) function call rintf($ he area + 'f s cm and circumference is 'f cm-n$, a , c !

    return !/

    void area_circum (double radius, double* area, double* circum " *area + PI * radius * radius!

    *circum + 0 * PI * radius!

    /

  • 8/11/2019 Lab08 Functions With Output Arguments

    2/3

    The function area circum is to return the area and circumference of a circle ($ results) given itsradius. *ince we want the function to return $ results, the return type will be void and we need $output arguments which must be pointers ( area and circum ). 'n the calling function (main), wedeclare $ normal variables ( a for area and c for circumference). +otice in the call, the actualarguments are the addresses of the variables a and c. nce the e ecution returns to the callingfunction (main), the values of area and circumference are stored in the variables a and c.

    Recursive function& recursive function is a function that calls itself. -ecursion is a powerful tool in problem solvingand programming. The strategy in recursive solutions is called divide and con"uer. The idea is tokeep reducing the problem si/e until it reduces to a simple case which has an obvious solution.-ecursive functions generally involve an if statement with the following form!

    i" this is a simp e case $%ase caseso !e it

    e sere'e"ine the prob em using recursion

    -edefine the problem using recursion means e press the solution of the current problem in terms of asolution of a similar problem but with smaller si/e. e keep repeating this process until we reach a

    base case ( ()pansion phase ). *ince the solution for a base case is known, now we do backsubstitution until we go back to the original case for which we were seeking the solution(Substitution phase ).-ecursive version of factorial function

    lon int factorial (int n"

    if (n ++

    return 1! else return n * factorial (n21 !/

    Exercises:1) rite a program that computes the area and perimeter of a rectangle using $ functions. nefunction is used to read the width and length, and the other to compute the area and perimeter.rite a main function to test your functions.

    2) rite a function that receives a time in seconds and returns the e"uivalent time in hours, minutes,and seconds. rite a main function to test your function. For e ample if the received time is 0111seconds, the function returns # hour, 2 minutes, and 01 seconds. Hint! 3se integer division andremainder.

    3) rite a recursive function Reverse that receives a positive integer number and prints it on thescreen in reverse order as shown below. rite a main function for testing.

  • 8/11/2019 Lab08 Functions With Output Arguments

    3/3

    4) rite a recursive function CountDigits that receives an integer number and returns how manydigits it contains. rite a main function to read an integer number and tests your function.

    +ote! For problems 4 5 0, you have to think recursively, like this!

    -everse( ) 6 if is a one digit number

    -everse( ) 6 ones digit of followed by -everse( 7 #1)