Download presentation
Presentation is loading. Please wait.
Published byJason Booth Modified over 9 years ago
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.