case study
What is the difference between a function with a return value and a function without a return value? & When is each of these functions used?
Return it or Not
A. Functions with Return Values
This function is useful for performing a process that requires a return value or OUTPUT. This function must define the data type of the value to be returned. This function is also called a Non-Procedural Function.
Its characteristics:
- Without void keyword
- There is a return keyword
- There is a data type that starts the function
- Have a return value
- It can be analogized as a variable that has a certain data type, so that the results can be displayed directly.
- Example declaration: int sum(int a, int b)
Example:
#include<iostream.h>
// Prototipe fungsi
Long kuadrat (long i);
--------------------------------------
//Definisi fungsi
Long kuadrat (long i)
{
return (i*i)
}
The return statement in the function above is used to return the square() function, where the square formula is obtained from the product of the number itself, for example i squared = ixi, as defined in the function above.
Another example:
Still with the same principle, namely multiplying the number itself, but the implementation is different, this time we define it in the square area formula.
C++ Area of Square Return Value
#include <iostream.h>
#include <conio.h>
//Ini merupakan fungsi dengan nilai balik (return).
//awal fungsi,
float luas(float s)
{
float luas;
luas=s*s;
return luas; //mengembalikan nilai ke fungsi
}
//akhir fungsi (fungsi utamanya)
void main()
{
float sisi;
float L;
cout<<"menghitung luas persegi"<<endl;
cout<<"Masukan sisi : "; cin>>sisi;
L=luas(sisi); //memanggil fungsi dan L menampung nilai dari fungsi
cout<<"Luas : "<<L;
getch();
}
B. Functions Without Return Value
It is a function that does not require a return value/output. This function has one purpose/focus often termed logical inherent or specific purpose. This function is also called a Procedural Function.
Its characteristics:
- No return keyword
- Using the void keyword
- There is no data type in the function declaration.
- The results cannot be displayed immediately.
- Has no function return value
- The void keyword is also used if a function does not contain any parameters.
- Example declaration: void display_jml (int a, int b)
So, the description of the differences between the two is as follows:
For example, if the definitions both use ixi, then the Return Value Function can be used to calculate the area of a square or the square of a value, while the Function Without Return Value can only apply to one of them, for example, the area of a circle is only the area of the circle, or if the square of the value is only the square of the value.
Example:
#include<iostream.h>
void tampilkan() // argumen kosong
{
cout<<"Terima Kasih telah mengunjungi blog saya"<<endl;
cout<<"Bla...Blaa..Bla.."<<endl;
cout<<"\t Silahkan tinggalkan komentar anda"<<endl;
getch();
}
Another example:
Using the i * i principle, but this function is dedicated only to the area of a square. See the difference in the source code.
C++ Area of Square Without Return Value
#include <conio.h>
#include <iostream.h>
//Ini merupakan fungsi tanpa nilai balik (void).
//awal fungsi,
void luas(float s)
{
float luas;
luas=s*s;
cout<<"luas : "<<luas<<endl;
}
//akhir fungsi (fungsi utamanya)
void main()
{
float sisi;
cout<<"menghitung luas persegi"<<endl;
cout<<"Masukan sisi : "; cin>>sisi;
luas(sisi); // memanggil fungsi
getch();
}
C++ Payroll Example with Return Value Function
Case study
A company PT. AMHARLE will create an employee payroll application with the following provisions:
Basic Salary (Gapok)
The basic salary is calculated based on length of service (years) as follows:
- <= 1Year = 850000
- 2 to 3 years = 1,000,000
- 4 to 5 years = 1250000
- > 5 Years = 2500000
Transportation Allowance
Transportation allowance is calculated per day at 25,000
Family Allowance
- Family allowance is given to married people amounting to 150,000 if they do not have children yet.
- If you already have children, 150,000 plus 75,000 per child (maximum number of children 3)
Order Bonus
Order bonus is calculated from the day of on-time entry of 20000
Bonus Level
The level bonus is determined as follows:
| Kode | Nama | Bonus |
|------|--------|--------|
| 1 | Junior | 125000 |
| 2 | Senior | 200000 |
| 3 | Expert | 350000 |
Total salary is the sum of Gapok, Transportation Allowance, Family Allowance, Order Bonus and Level Bonus.
Application screenshot
1\. Atur Tanggal Penggajian
2\. Entri Data
3\. List Data
4\. Exit
Pilihan Anda (1-3) ?
Display when menu 1 is selected: Set payroll date
Masukkan Tanggal, Bulan dan Tahun Penggajian
Tanggal:
Bulan:
Tahun:
Display when menu 2 is selected: Data Entry
Nama : .....
Lama kerja(tahun) : ....
Jumlah masuk kerja : ....
Jumlah masuk tepat waktu : ...
level karyawan (1/2/3) : ...
Status menikah (Y/T) : ...
Jumlah Anak : ....
Information:
Input for the number of children will only appear if the married status is filled in with y.
Display when menu 3 is selected: Data List
List gaji karyawan PT. AMHARLE Yogyakarta
Tanggal: 99 xxxxxxx 9999
+-----+-----------+--------+-------+-------+----------+-------+-------+--------+--------+
| No. | Nama | LK(Th) | Level | Gapok | T. Trans | T.Kel | B.Ket |B.Level | Total |
+-----+-----------+--------+-------+-------+----------+-------+-------+--------+--------+
| | | | | | | | | | |
+-----+-----------+--------+-------+-------+----------+-------+-------+--------+--------+
| Total gaji dikeluarkan | |
+-----+-----------+--------+-------+-------+----------+-------+-------+--------+--------+
Information:
- The date format is the date, month name and year.
- When menu 4 is selected, the display displays the profile of the application maker (e.g. Student ID, Name, etc.).
Program creation requirements:
- The program was created using Borland C++
- The program was created by utilizing array data types and was made modular by utilizing user defined functions/self-made functions.
Assessment Terms:
- Menu option 1: 10%
- Menu option 2: 25%
- Menu option 3: 60%
- Menu option 4: 5%
Terms of creation and submission of works:
- The program is created individually, not in groups.
- The above value provisions only apply to assignments resulting from one's own work. If cheating is found in the creation of the work, the lecturer is free to give a value without using the assessment provisions above.
- The deadline for submitting assignments is 10 days from the date this question was published.
Screenshot:
Main course
Option 1
Option 2
Option 3
Option 4
Source Code:
#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <iomanip.h>
#include <stdlib.h>
/**www.gatewan.com || (Wawan Beneran)**/
/*tempat mendeklarasikan prorotipe fungsi-fungsi dan
variabel-variabel yang sekiranya perlu dikenali secara global*/
//--------------------------------------------------------------------------
menu(); tgl(); entrydata(); long fgp(long gp); long tjsts(long tklrg);
long tjjmtw(long bket); list(); long bnslvl(long blvl); bye();
void cetak(int row, char *text); int bom(); back();
char nm[30], m[30], sts;
int lmkrj, jmk, jmtw, lvl, ank, d, pilihan;
long y, jgp, bkel, jbket, bolvl;
//------------------------------------------------------------------------
Comment 1
- PAYROLL SOFTWARE Jul 25, 2015, 20:51:00 = Bro, is the payroll software still being updated?
- ARI WIBISONO Mar 9 2016, 12:21:00 = long and gotoxy(3,10) what are the parameters for, bro?
Response 1
- If I ever come across a similar case study, I will probably update it, bro.
- long is a data type for variables, while gotoxy is used to set the position of strings, characters or variable contents that will be displayed on the CLI screen, where the dimensions of the CLI screen are not like pixel x pixel graphics, but text mode, namely 80x25 (character x character) or (x, y), so gotoxy has parameters (x, y) as in the Cartesian diagram x is horizontal and y is vertical. that's it, thanks