Professional Documents
Culture Documents
LECTURER’S NAME:
GROUP NO. :
DATE SUBMIT :
PRAC. TOTAL
REPORT
STUDENT ID & NAME : SKILL MARKS
(20%)
(80%) (100%)
(1)
(2)
(3)
Student 2
Score 1 2 3 4 5
Score
Aspect Weak Average Satisfactory Good Excellent
Able to write
part of the
Able to write
program
program and Able to write
Write Unable to Able to write correctly but
Program
gives correct program without
write program less than three
output under lecturer’s x6
PRACTICAL SKILL (80%)
Accurate
Accurate statement of the
No statement of the results of lab
A statement of
conclusion A statement of results of the lab indicates
the results of
was the results is indicates whether results
the lab
included or incomplete whether results support
Conclusion indicates
shows little with little support the hypothesis
whether x1
effort and reflection on hypothesis Possible
results support
reflection on the lab Possible sources of error
the hypothesis
the lab sources of error and what was
identified learned from the
lab discussed
1-2 3-4
References 0 reference 4-5 references >5 references x1
references references
Total 100
1 LEARNING OUTCOMES (LO):
At the end of this session, student should be able to:
1. Apply Function method in solving problem in C programming.
2. Builds flowchart to solve programming problem before coding.
2 OBJECTIVES
1. Learn how to fix, complete and manipulates the Function method.
2. Learn how to builds flowchart to solve programming problem before coding.
3 THEORY
FUNCTION
A function is a block of statements that performs a specific task. Suppose you are
building an application in C language and in one of your program, you need to perform a
same task more than once. In such case you have two options –
a) Use the same set of statements every time you want to perform the task
b) Create a function to perform that task, and just call it every time you need to
perform that task.
Types of functions
a) Predefined standard library functions – such as puts(), gets(), printf(), scanf() etc
– These are the functions which already have a definition in header files (.h files
like stdio.h), so we just call them whenever there is a need to use them.
b) User Defined functions – The functions that we create in a program are known
as user defined functions.
By user defined function, there are two methods to pass the data into the function in C
language, i.e., call by value and call by reference.
4 EQUIPMENT / TOOLS
Computer, C programming language ( Dev-cpp, codeblock, dll)
Program 1
#include<stdio.h>
void change(int num) // function
{
printf("Before adding value inside function num=%d \
n",num); num=num+100;
printf("After adding operation inside function num=%d \n",num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(x);//calling change() function and passing value x to
it printf("After function call x=%d \n", x);
return 0; }
Program 2
#include<stdio.h>
void change(int
*num)
{
printf("Before adding value inside function num=%d \
n",*num); (*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//calling change() and passing an reference(address)x to
it printf("After function call x=%d \n", x);
return 0;
}
Built and Run Program 2 using a C compiler
Output: Discuss the output value inside main() and inside function
change()
Practical Assignments 1
Fix the Program:
include <stdio.h>
int addition(int num1, int num2)
{
char sum;
sum = num1+num2;
return num1;
}
int main()
{
int var1,var2,res;
printf("Enter number 1:
"); scanf("%c",var1);
printf("Enter number 2:
"); scanf("%d",var2);
res); return 0;
}
Write the program fixed:
Output :
Practical Assignments 2
Fix the Program:
/* C Program below is supposed to swap values of two integer.
Before it could be used, you need to look up for five errors. Find
out the errors and fix it*/
#include <stdio.h>
void swap(int *, int b); //prototype of the
function int main()
{
int a =
10; int b
= 20;
printf("Before swapping the values in main a = %d, b = %d\
n",a,b);
// printing the value of a and b in
main swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b); //
The values of actual parameters do change in call by reference, a =
10,
b = 20
}
void swap (int *a, int *b)
{
int
temp;
temp =
a;
*a=*b
;
b=tem
p;
printf("After swapping values in function a = %d, b = %d\
n",*a,*b)
; // Formal parameters, a = 20, b = 10
}
Write the program fixed:
Output :
Practical Assignments 3
Complete the Program:
/* The objective of the C program below is to compare two input
numbers and display which one is bigger. It has five blanks that
you should fill up in order to work */
int main ()
{
/* local variable definition
*/ int nom1;
int
nom2;
int
ret;
printf("Input a number :
"); scanf("%d",&nom1);
printf("Input another number :
"); scanf("%d",&nom2);
ret =max(nom1,nom2); /* calling a function to get max value */
printf("Max value is : \n", );
return 0;
}
Write the completed Program :
Output :
Practical Assignments 4
Complete the Program:
/*The c program is used to swap any two integer entered by a user,
It has five blanks that you should fill up in order to work */
#include
void swapnum ( int *var1, int *var2 )
{
int tempnum ;
tempnum = *var1
;
*var1 = ;
*var2 = tempnum ;
}
int main( )
{
int num1, num2;
printf("\n Please enter one integer
number :”); scanf(“ ”,&num1);
printf("\n Please enter another integer
number :”); scanf(“%d”, );
printf("Before swapping:");
printf("\nnum1 value is %d",
num1); printf("\nnum2 value is
%d", num2);
printf("\nAfter swapping:");
printf("\nnum1 value is %d",
num1); printf("\nnum2 value is
%d", num2); return 0;
}
Write the completed Program :
Output:
Discussion :
Conclusion:
References: