1. UNIVERSITI TUN HUSSEIN ONN MALAYSIA
FACULTY OF MECHANICAL AND MANUFACTURING ENGINEERING
BTI 10202: COMPUTER PROGRAMMING
LAB 9 : Functions
NAME : _____________________________________
MATRICS NO.: _______________ DATE : ____________ MARK
Objective: Introduce to the usage of a function, types of function and function
Theory:
1. A function is a section of program that perform specific task. For an example; in real life, a
big project is split into sub project (e.g: contactor manage subcontract to finish a building).
The same method can be used to segregate certain task in programming.
a. Main function: to call sub function accordingly
b. Sub function one: to receive information and calculate
c. Sub function to : to display the result
2. Function definition: consist of two parts:
a. Function header: divided into three parts:
Prototypes declaration example:
Return_type function_name (parameters_list);
i. A return type
• Similar to variable type: int (round number), char (letter and
strings), float/double (decimal numbers), void (no returning value).
ii. A function name
• Use easy to understand sub function name that complies with the
syntax rules
iii. Parameter lists
• The list of data for communication purpose with the calling
functions. If there is no data to be use, the keyword void is use.
b. Function body normally contains:
i. Local declaration
ii. Local definition
iii. Return expression (this does not apply to function without any returning
value)
3. Function call is a postfix expression to call a sub functions/how to call function from any
functions:
Example:
printf(“The result is %d”, add(a,b)); //a function can be call from printf
display();//direct instruction to jump to the sub function
total=add(a,b); //assigning sub function add to total
2. Exercise 1: type the coding below and answer the following questions:
1 #include<stdio.h>
2 #include<conio.h>
3
4 int fnAdd(int iValue1, int iValue2);
5
6 int main() {
7 int iResult,iValue1=8, iValue2=9;
8
9 iResult = fnAdd(iValue1, iValue2);
10 printf("Sum of %d and %d is %dn",iValue1, iValue2, iResult);
11 getch();
12 return 0; }
13
14 int fnAdd(int iValue1, int iValue2)
15 {
16 int iSum;
17 iSum = iValue1 + iValue2;
18 return (iSum); }
i. What is the output of the coding? Output:
ii. Change line 4 with int fnAdd(int iNum1, int iNum2) ;
line 14 with int fnAdd(int iNum1, int iNum2) and
line 17 iSum = iNum1 + iNum2;
Compile and run the coding. Explain how this change affects the output. Output:
_____________________________________________________________________
_____________________________________________________________________
iii. Revert to original coding. Remove coding in line 9
Change line 10 with printf("Sum of %d and %d is %dn",iValue1, iValue2,
fnAdd(iValue1, iValue2));
Compile and run the coding. Explain how this change affects the output. Output:
Explains what you learn:
_____________________________________________________________________
_____________________________________________________________________
iv. Change line 7 with float iResult,iValue1=8, iValue2=9;
3. List the error and explains:
_____________________________________________________________________
_____________________________________________________________________
_____________________________________________________________________
v. Revert to original coding. Change line 14 with int fnAdd(inum1, inum2)
List the error and explains: