Presentation is loading. Please wait.

Presentation is loading. Please wait.

Functions with Input/Ouput. Assignments Due – Lab 2 Reading – Chapter 4 – 4.1-4.4.

Similar presentations


Presentation on theme: "Functions with Input/Ouput. Assignments Due – Lab 2 Reading – Chapter 4 – 4.1-4.4."— Presentation transcript:

1 Functions with Input/Ouput

2 Assignments Due – Lab 2 Reading – Chapter 4 – 4.1-4.4

3 Functions with Arguments Subproblems may require input and/or generate output funcswithargs.c return_type name(type name, … type name) { }

4 Input Argument Only print_total_cost(total); //function call //function definition void print_total_cost(double total_amt) { printf("The total for your purchase is %5.2lf\n", total_amt); } copy the value from total to a new variable called total_amt total_amt is only valid for the life of print_total_cost

5 Output Result Only cost = get_cost(); //function call double get_cost() //function definition { double itemcost; printf("Enter cost of item: "); scanf("%lf", &itemcost); return itemcost; //return statement } value of function call is the returned value if return type – must have return statement itemcost is a variable local to the get_cost function

6 Input and Output total = get_taxed_amt(cost); double get_taxed_amt(double cost) { double tax; double total; tax = cost * TAX_RATE; total = cost + tax; return total; } two different cost variables

7 Multiple Arguments total = get_taxed_amt(cost,.05); double get_taxed_amt(double cost, double tax_rate) { double tax; double total; tax = cost * tax_rate; total = cost + tax; return total; } argument in function call may be variable name OR value

8 Execution of Functions


Download ppt "Functions with Input/Ouput. Assignments Due – Lab 2 Reading – Chapter 4 – 4.1-4.4."

Similar presentations


Ads by Google