ITP Exp7
ITP Exp7
OBJECTIVES
The main purpose of the experiment is to introduce you to C functions. In this experiment,
firstly, definition of the C functions is given. Secondly, function prototype, function call and
function definition concepts are explained. Lastly, simple function examples are studied.
INFORMATION
C Functions
On the other hand, to solve real-life problems thousands of statements are required. The best
way to develop a large program is to combine it from smaller and manageable block of codes
(functions). In that hierarchy, each function should be responsible a specific task and the
management, development and debugging of the large program could be easier than
straightforward-fashioned programs. This property is also simplicity (Fig. 2). The last advantage
of the usage of functions is abstraction. The abstraction allows creating standard functions with
well-chosen function name and definition.
E-mail : [email protected]
Introduction To Programming Laboratory, Fall 2020
Basically, there are two types of C functions. One of them is standard functions that are
placed in the C Standard Library. The C Standard Library includes functions to perform
mathematical calculations, string and character manipulations, input/output, and time operations
[3]. These standard functions provide developing large programs with the abstraction properties
of the functions. Another kind of function is programmer (or user)-defined functions. These
functions are created by the programmer to achieve a task. In order to use programmer-defined
functions, the program should include function prototype which informs the compiler about the
function. Also, functions must be invoked by a function call which specify function name,
information used in the function (arguments) and results of the performed task. Lastly, function
definition includes statements to complete the task. In the next subsection, a programmer-
defined function is analyzed.
E-mail : [email protected]
Introduction To Programming Laboratory, Fall 2020
Consider the function example program that includes cube function to calculate the cube of
the given number. A function prototype informs the compiler the type of data returned by the
function, the number of parameters the function expects to receive, the types of parameters, and
the order in which parameters are expected. Additionally, the compiler refers to the function
prototype to check that function call contains the correct return type, the correct number of
arguments, the correct argument types, and that the arguments are in the correct order [3].
Line 12 shows the function prototype. The cube function receives an integer value and returns
also an integer value. cube function is called by line 24. As seen from the figure, both the
number and result variables are in integer type. Lastly, definition of the cube function begins
with line 32. cube function expects an integer parameter x. The keyword int preceding the
function name indicates that the function returns an integer result. In line 32, cube function
receives a copy of the value of the number into parameter x. Then, function calculates and return
cubes of the entered number.
QUESTIONS
1) Write a C program to convert the miles to kilometers. The program must include the
mileToKm function that receives miles as in double type and returns kilometers also in double
type. (Hint: 1 mile is equivalent 1.61km)
2) The binary number system forms only 0 and 1 digit. Write a C program to calculate the
decimal equivalent of a binary number. Use four digit binary numbers and for each digit prompt
the number, store it in different variable. Then write the binaryToDecimal function that receives
four digit binary number and return an integer number. (Example: Binary number is 1011.
Decimal is calculated as 1x23+0x22+1x21+1x20=11)
∑ 𝑎𝑟 𝑛−1 = 𝑎 + 𝑎𝑟 + 𝑎𝑟 2 + ⋯
𝑛=1
is called a geometric series. The number a is the first term. The number r is called the common
ratio of the series. The nth partial sum sn of a geometric series is calculated as follows:
2 𝑛−1
𝑎(1 − 𝑟 𝑛 )
𝑠𝑛 = 𝑎 + 𝑎𝑟 + 𝑎𝑟 + ⋯ + 𝑎𝑟 =
1−𝑟
Write a C program that prompts the first term a, common ratio r, and number of item n then
calculates the nth partial sum sn of a geometric series. The program must include the geoSeries
function that receives a, r, and n in double type and then returns the nth partial sum sn of a
geometric series also in double type.
E-mail : [email protected]