Thread: Call Functions- What now??

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Call Functions- What now??

    I need help with this, we spent 2 days in class talking about pass by address and Pass by value. But I dont wht or how to do with this lab assignment, I am not asking you to do the coding just kind of a schematics of where the address are suppose to go reallyI have no clue what to do. Any way of explaining what the heck the instructor wants???? I got th first part done easily but pass what address where?? HUH?

    The program still asks for a price, calculates the total cost, the change, and the dollars and cents in change. However, the program has a main function which calls the following 2 functions:
    A function to ask the user for a price, and calculate the total cost. The function sends the total cost back to main so main will print the total cost.
    A function to ask the user for a payment, calculate and print the change. Then the function calls 2 sub-functions:
    o A function to calculate and pass back the number of each type of dollar bill, so that the calling function will print the number of bills.
    o A function to calculate and pass back the number of each type of coins, so that the calling function will print the number of coins.

    Draw a structure chart of your program before coding. You’re encouraged to have me review your structure chart before you start coding, so you don’t code the wrong design and not get credit for it.

    Test your program the same way with 2 test cases:
    Large change amount (a couple $20 at least)
    Small change amount (a few nickels and pennies)


    For each of the 4 functions above, write a short explanation (1 or 2 sentences) why you choose pass by value or pass by address for the input to the function. To get the extra credit, you must be specific about why you choose pass by value or pass by address (“I choose pass by address because it works” is not a good reason). Put the explanation as a comment before each function.
    ********************SOURCE CODE**********************
    Code:
    #include <stdio.h>
    
    #define TAX 9.25f
    #define TWENTY_DOLLAR 20
    #define TEN_DOLLAR 10
    #define FIVE_DOLLAR 5
    #define	ONE_DOLLAR	1
    #define QUARTER .25f
    #define DIME 0.1f
    #define NICKLE 0.05f
    #define	PENNY 0.01f
    #define PENNY_FUDGE_FACTOR PENNY/2.0
    
    int main(void)
    {
    
    	// in a function call: there should be no data type
    	// which means there should be no blue text 
    
    	//num = getInput (); // PARENTESES =Function Call  no input, 1 output
    //	result = triple (num); // 1 input, 1 output
    	//print (num, result, ); // 2 input, no output
    
    
    	/* local definitions */
    	float price ;
    	float payment; 
    	float change ; 
    	float tax;
        float total;
    	
    	
    	/* statements */
    	
    	
    	printf ("Enter the price: $");
    	
    	scanf ("%f", &price); 
    	
    	
    	tax = price * TAX/100; 
    	total= tax + price;
    	
    	printf("Price plus tax is $%2.2f", total);
    	/*asking for the payment and change*/
    	
    	
    	printf("\nPlease enter the payment: $");
    	scanf ("%f", &payment); 
    	//payment = 50;   for testing..
    	
    	// Compute the change:
    	change = payment - total;
    	
    	printf("Change is %2.2f ", change);
    	
    	printf("\nWhich is\n");
    	
    	// Compute the breakdown:
    	
    	// Twenty dollar bills:
    	int b = (int)change / TWENTY_DOLLAR;
    	printf("%d $20\n", b);
    	
    	change = change - b * TWENTY_DOLLAR;
    
    	// Ten dollar bills:
    	b = (int)change / TEN_DOLLAR;
    	printf("%d $10\n", b);
    
    	change = change - b * TEN_DOLLAR;
    	
    	// Five dollar bills:
    	b = (int)change / FIVE_DOLLAR;
    	printf("%d $5\n", b);
    	
    	change = change - b * FIVE_DOLLAR;
    
    	// One dollar bills:
    	b = (int)change / ONE_DOLLAR;
    	printf("%d $1\n", b);
    	
    	change = change - b * ONE_DOLLAR;
    	
    	// Quarters:
    	b = change / QUARTER;
    	printf("%d 25 cents\n", b);
    	
    	change = change - b * QUARTER;
    	
    	// Dimes:
    	b = change / DIME;
    	printf("%d 10 cents\n", b);
    	
    	change = change - b * DIME;
    
    	// Nickles:
    	b = change / NICKLE;
    	printf("%d 5 cents\n", b);
    	
    	change = change - b * NICKLE;
    
    	// Pennies:
    	b = (change + PENNY_FUDGE_FACTOR) / PENNY;
    	printf("%d 1 cent\n", b);
    	
    	change = change - b * PENNY;	
    
    	return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Function passing in a value...
    Code:
    int function(int x)
      { return x * 2; }
    
    // call as 
    int d;
    int y;
    
    d = 4;
    y = function (d);
    same function using an address...

    Code:
    int function(int *x)
      { return *x * 2; }
    
    // call as 
    int d;
    int y;
    
    d =4; 
    y = function (&d);
    The first function gets the value of d (4).
    The second one gets the address of d (could be anything) and extracts the value internally.

    For more depth, look up pointers and functions in any good C language tutorial.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    33
    in c everything is passed by values.
    when pointers are passed the values of the address are passed and indirection operator is used to acess the values.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. call functions in C++ dll from C program
    By Amera in forum C Programming
    Replies: 0
    Last Post: 11-18-2009, 11:02 PM
  2. Passing functions call by value?
    By yougene in forum C Programming
    Replies: 2
    Last Post: 12-19-2008, 04:56 AM
  3. How do I call these functions?
    By Todd88 in forum C++ Programming
    Replies: 24
    Last Post: 11-27-2008, 12:21 AM
  4. Call Back functions
    By ladhnur in forum C Programming
    Replies: 8
    Last Post: 08-22-2002, 08:58 AM
  5. how to call a DLL functions in VC++
    By mjmariveles in forum Windows Programming
    Replies: 4
    Last Post: 07-08-2002, 08:57 PM

Tags for this Thread