Download Functions 1 - Portal UniMAP

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Falcon (programming language) wikipedia , lookup

Name mangling wikipedia , lookup

Structured programming wikipedia , lookup

Functional programming wikipedia , lookup

Recursion (computer science) wikipedia , lookup

Subroutine wikipedia , lookup

Dirac delta function wikipedia , lookup

APL syntax and symbols wikipedia , lookup

C syntax wikipedia , lookup

Function object wikipedia , lookup

C++ wikipedia , lookup

Standard ML wikipedia , lookup

Transcript
EKT150 – Introduction to Computer Programming
Laboratory Module
LAB 6
FUNCTIONS I
School of Computer and Communication Engineering
Universiti Malaysia Perlis
1
EKT150 – Introduction to Computer Programming
Laboratory Module
1. OBJECTIVES:
1.1 To apply functions as building blocks of programs.
1.2 To write C programs using functions.
2 INTRODUCTION:
A C program is generally formed by a set of functions, which subsequently consist of
many programming statements. Using functions, a large computing task can be broken
into smaller ones. Functions can be created to execute small, frequently-used tasks. In
C, there are predefined functions or sometimes called standard functions, and userdefined functions.
Predefined functions are already available functions which can be used, called library,
such as stdio.h, math.h, string.h and stdlib.h. The library name must be included at
the top of the source code (preprocessor directive).
User-defined functions in a program are built using:
 Function prototype
 Function definition
 Function call
2.1 Function Prototype
To use function in a program, it has to be declared at the beginning of a program, using
function prototype. Function prototype has the following form:
<return_type> <function_name> (arg_type
arg_name, ...);
For example:
int sum (int num1,int num2); //function named sum with two (2) arguments
and returns integer data type.
void sum (int num1,int num2); //function named sum with two (2) arguments
but does not return any data.
2.2 Function Definition
Function definition is the function body. It is used to define what the function does. The
coding is written inside the function definition. Function definition has the following form:
<return_type> <function_name> (arg_type arg_name, ...)
{
… statements …
}
For example:
int sum (int num1,int num2)
{
int add;
add = num1 + num2;
return(add);
}
2
EKT150 – Introduction to Computer Programming
Laboratory Module
2.3 Function Call
Function call can be made in the main function or in other functions. Function call has
the following form:
<function_name> (exp, exp ...)
 exp is an expression – can be variable or constant
For example: result = sum(x,y);
3. TASKS:
3.1 Example of a function that returns a value. Compile, execute and understand the
program below:
//This program sums up two numbers
#include <stdio.h>
int sum (int, int);
//function prototype
int main()
{
int x,y, result;
printf(“Enter x and y : ”);
scanf(“%d %d”, &x, &y);
result = sum(x,y);
printf(“\nSum is : %d\n”, result);
return 0;
}
int sum (int num1, int num2)
{
int add;
add = num1+num2;
return(add);
}
//variables declaration
//function call
//function definition
//return the result of summation
//two numbers to the main function.
Write the output of the program.
3
EKT150 – Introduction to Computer Programming
Laboratory Module
3.2 Suppose that you have the following program of the main function with the declaration of
the prototypes. Complete the program by defining and building the four (4) functions as the
declared prototypes.
#include <stdio.h>
int add(int,int);
int subtract(int,int);
int multiply(int,int);
void print_result(int);
//function prototype
//function prototype
//function prototype
//function prototype
int main()
{
int num1,num2,answer;
char op;
printf(“Enter two numbers and operator: ”);
scanf(“%d %d %c”, &num1,&num2,&op);
switch(op)
{
case ‘+’ : answer=add(num1,num2); break;
case ‘-’ : answer=subtract(num1,num2); break;
case ‘*’ : answer=multiply(num1,num2); break;
default : printf(“Invalid operator”); break;
}
print_result(answer);
return 0;
}
4
//function call
//function call
//function call
//function call
EKT150 – Introduction to Computer Programming
Laboratory Module
3.3 Write a program using a function named ‘even’ that checks whether a number entered by
user is an even number. Your program could also use a loop to count the quantity of even
and odd numbers entered by the user.
5
EKT150 – Introduction to Computer Programming
Laboratory Module
4. ADDITIONAL TASK:
Write a C program that calculates a customer water bill. The water bill includes RM 5.00
basic cost and cost for water usage with rate of RM 1.10 per thousand liter. Water usage is
calculated by subtracting current meter reading with previous month meter reading (meter is
read in thousand liter unit).Your program should check whether there is an unpaid bill. If the
balance of unpaid bill is greater than 0, RM 2.00 fine is charged together with the unpaid bill
and this will be included in the current monthly bill. The program will also calculate the bill
collected for the day. The program will continue if user enters y- yes, else it will stop and
display the collection for the day. Your program should use these functions :
1.
2.
3.
4.
calc_usage_cost - accepts previous and current meter reading, returns usage cost.
calc_unpaid_cost - accepts unpaid bill, returns unpaid cost.
calc_total_bill - accepts usage cost and unpaid cost, returns total bill.
print_bill - accepts account number and total bill.
Sample Output
-----------------Perlis Water---------------------This program generates monthly water bill
------------------------------------------------------Enter account number : 44444
Enter unpaid bill: 0
Enter previous month and current month meter reading: 1000 2000
Your account number is 4444
Your total bill is RM 6.10.
Do you want to continue : y or n ? y
Enter account number : 22222
Enter unpaid bill: 10
Enter previous month and current month meter reading: 1000 2000
Your account number is 2222
Your total bill is RM 18.10.
Do you want to continue : y or n ? n
Perlis Water collection : RM 24.20.
6