Using a Conditional inside a User-Defined Function
If you want to give the viewer a choice of options, this can be handled either inside a user-defined function, or in the main( ) function. Let’s begin by handling viewer choice inside a user-defined function.
In this example, a customer chooses how many books to order. The company uses this chart to calculate shipping charges:
Books Ordered1-5 |
Shipping Cost$2.75 per book |
First, we’ll make a user-defined function to calculate the shipping charges, based upon how many books the viewer orders.
Since the value returned will be a float (for money), use a float function called Ship. Pass the number of books the viewer orders, using an integer variableamt. Then use if statements and returns to calculate the shipping for the number of books ordered:
#include<stdio.h>
float Ship(int amt)
{
if(amt <= 5)
{
return(amt * 2.75);
}if(amt <=10)
{
return(amt * 2.00);
}if(amt <=15)
{
return(amt * 1.25);
}if(amt <=20)
{
return(amt * 0.75);
}else
{
return 0;
}}
Second, we’ll declare our variables, ask the viewer how many books they want to order, pass the amount to the function, then print the result:
void main( )
{
int amt;
float shipping;
printf(“How many books do you want to order? “);
scanf(“%d”, &amt);
shipping = Ship(amt);
printf(“Books Ordered:\t%d”, amt);
printf(“\nShipping Cost:\t$%.2f”, shipping);
}
Here is one possible result from this program:
How many books do you want to order? 14
Books Ordered: 14
Shipping Cost: $17.50
Using a Switch outside a User-Defined Function
If you don’t want to use a conditional inside a user-defined function, you can write a conditional, loop, or switch in the main( ) function, then pass the result to the user-defined function. For example, we want to create a program to convert temperatures. The viewer can convert Fahrenheit to Celcius, or Celcius to Fahrenheit.
First, create a user-defined function to convert Fahrenheit to Celcius:
#include<stdio.h>
int FC(int temp)
{
printf(“\nFahrenheit\n%d\nCelcius\n”, temp);
return ((temp – 32) * 5/9);
}
Second, create a function to convert Celcius to Fahrenheit:
int CF(int temp)
{
printf(“\nCelcius\n%d\nFahrenheit\n”, temp);
return ((temp * 9/5) – 32);
}
Third, in the main( ) function, declare your variables and ask the viewer what type of coversion they want:
void main( )
{
int temp;
int result;
char choice;
printf(“Do you want to convert:\n”);
printf(“Fahrenheit to Celcius (type f)\n”);
printf(“Celcius to Fahrenheit (type c)? “);
scanf(” %c”, &choice);
Fourth, in the main( ) function, write a switch to decide which function will be called,Fahrenheit to Celcius or Celcius to Fahrenheit:
switch(choice)
{
case ‘f’:
printf(“\nType a temperature in Fahrenheit: “);
scanf(“%d”, &temp);
result = FC(temp);
printf(“%d”, result);
break;case ‘c’:
printf(“\nType a temperature in Celcius: “);
scanf(“%d”, &temp);
result = CF(temp);
printf(“%d”, result);
break;default:
printf(“You made a mistake. Type either f or c.\n”);
break;
}
}
Here is one possible result from this program:
Do you want to convert:
Fahrenheit to Celcius (type f)
Celcius to Fahrenheit (type c)? fType a temperature in Fahrenheit: 85
Fahrenheit
85
Celcius
29
Leave a comment