LAB # 10 HANDOUT
Objectives:
Functions:
When coding long programs, it is advisable to use functions. Functions are subpart of a program which can
be used whenever they need to be. This saves us the trouble of writing (or copying) same code over and over
again when we know that it has to perform the same “function”.
Another way to think of a function is to think of it as an employee. Each employee has a specific job and it is
capable of doing it over and over again as many times as need be. Another employee may have a different
function but the same holds true for it too i.e. it is capable of performing that function over and over again.
Continuing with the boss-employee analogy, there are a few things that need to be noted: one, the boss usually
gives something to the employee to work on (for example, a file). When the employee has finished working
on it, he returns something to the boss (again the file). The file of the things that are given by the boss
(user/another function) to the employee are called parameters while the thing that the employee (function)
returns is called the return value.
Calling a function in main() or another function is as simple as writing its name and passing the arguments to
the function. A variable may be needed to “catch” the return value if the function returns something.
Syntax:
return data type function_name (parameter
list)
{ //Some Code
return value; }
int add (int i, int j)
{ int k =0;
k = i+j;
return k; }
Practice questions:
1. Write a function that takes two numbers as parameters and returns their sum.
2. Modify the first program to also take a character as one of the parameters and depending upon this
parameter performs the basic 4 mathematical functions (+, -, *, /).
3. Write a function that takes length of a side of a square as input and returns the area.
4. Modify the above task so that there is no return value but the answer is displayed anyway.
5. Write a program that takes 4 numbers as parameters and returns the biggest number out of the four
numbers.
6. Write a program that will convert miles to kilometers and kilometers to miles. The user will indicate both
a number (representing a distance) and a choice of whether that number is in miles to be converted to
kilometers or kilometers to be converted to miles. Each conversion is done with a value returning function.
You may use the following conversions: 1 kilometer = .621 miles, 1 mile = 1.61 kilometers

Handout # 3 functions c++

  • 1.
    LAB # 10HANDOUT Objectives: Functions: When coding long programs, it is advisable to use functions. Functions are subpart of a program which can be used whenever they need to be. This saves us the trouble of writing (or copying) same code over and over again when we know that it has to perform the same “function”. Another way to think of a function is to think of it as an employee. Each employee has a specific job and it is capable of doing it over and over again as many times as need be. Another employee may have a different function but the same holds true for it too i.e. it is capable of performing that function over and over again. Continuing with the boss-employee analogy, there are a few things that need to be noted: one, the boss usually gives something to the employee to work on (for example, a file). When the employee has finished working on it, he returns something to the boss (again the file). The file of the things that are given by the boss (user/another function) to the employee are called parameters while the thing that the employee (function) returns is called the return value. Calling a function in main() or another function is as simple as writing its name and passing the arguments to the function. A variable may be needed to “catch” the return value if the function returns something. Syntax: return data type function_name (parameter list) { //Some Code return value; } int add (int i, int j) { int k =0; k = i+j; return k; } Practice questions: 1. Write a function that takes two numbers as parameters and returns their sum. 2. Modify the first program to also take a character as one of the parameters and depending upon this parameter performs the basic 4 mathematical functions (+, -, *, /). 3. Write a function that takes length of a side of a square as input and returns the area. 4. Modify the above task so that there is no return value but the answer is displayed anyway. 5. Write a program that takes 4 numbers as parameters and returns the biggest number out of the four numbers. 6. Write a program that will convert miles to kilometers and kilometers to miles. The user will indicate both a number (representing a distance) and a choice of whether that number is in miles to be converted to kilometers or kilometers to be converted to miles. Each conversion is done with a value returning function. You may use the following conversions: 1 kilometer = .621 miles, 1 mile = 1.61 kilometers