Home > Tutorials > C Tutorials > Programming In C: Functions with Viewer Choice

Programming In C: Functions with Viewer Choice

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 Ordered

1-5
6-10
11-15
16-20
21+

Shipping Cost

$2.75 per book
$2.00 per book
$1.25 per book
$0.75 per book
free shipping

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)? f

Type a temperature in Fahrenheit: 85

Fahrenheit
85
Celcius
29

About Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Check Also

How To Populate A DIV via an HTML SelectBox, PDO, and jQuery

Imagine you have a database full of information, and you want to let users browse ...

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close