You are on page 1of 8

Functions with Input/Ouput

Assignments
Due Lab 2

Reading Chapter 4 4.1-4.4


Functions with Arguments
Subproblems may require input and/or
generate output

funcswithargs.c

return_type name(type name, type name)


{
}
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
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
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


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


Execution of Functions

You might also like