Tutorial dan Contoh Structure and Function Pemrograman C++

Pada kesempatan ini Mycoding.net berbagi artikel tentang yang berjudul .

Structure variables can be passed to a function and returned from a function in similar way as normal arguments:
Returning structure from function in C++, Passing structure to function in C++ and C++ Structure Function

Passing structure to function in C++

A structure variable can be passed to a function in similar way as normal argument. Consider this example:

Example 1: C++ Structure and Function

#include <iostream>
using namespace std;
struct person {
char name[50];
int age;
float salary;
};
void displayData(person); // Function declaration
int main() {

person p
;

cout
<< "Enter Full name: ";
cin
.get(p.name, 50);
cout
<< "Enter age: ";
cin
>> p.age;
cout
<< "Enter salary: ";
cin
>> p.salary;

displayData
(p); // Function call with structure variable as arugment

return 0;
}
void displayData(person p1) {

cout
<< "\nDisplaying Information." << endl;
cout
<< "Name: " << p1.name << endl;
cout
<<"Age: " << p1.age << endl;
cout
<< "Salary: " << p1.salary;
}
Output
Enter Full name: Bill Jobs
Enter age: 55
Enter salary: 34233.4

Displaying Information.
Name: Bill Jobs
Age: 55
Salary: 34233.4
In this program, user is asked to enter the data for the members of a structure variable inside main() function. Then that structure variable to passed to a function using code.
displayData(p);
The return type of displayData() is void and one argument of type structure person is passed. The function prototype also should match the function definition. Then the members of structure p1 is displayed from this function.

Returning structure from function in C++

#include <iostream>
using namespace std;
struct person {
char name[50];
int age;
float salary;
};

person getData
(person); void displayData(person); int main() {

person p
;

p
= getData(p);
displayData
(p);

return 0;
}

person getData
(person p1) {

cout
<< "Enter Full name: ";
cin
.get(p1.name, 50);
cout
<< "Enter age: ";
cin
>> p1.age;
cout
<< "Enter salary: ";
cin
>> p1.salary;
return p1;
}
void displayData(person p1) {

cout
<< "\nDisplaying Information." << endl;
cout
<< "Name: " << p1.name << endl;
cout
<<"Age: " << p1.age << endl;
cout
<< "Salary: " << p1.salary;
}
The output of this program is same as program above.
In this program, the structure variable p of type structure person is defined under main() function. The structure variable p is passed to getData() function which takes input from user and that value is stored in member function of structure variable p1 which is then returned to main function.
p = getData(p); 
 
Note: The value of all members of a structure variable can be assigned to another structure using assignment operator = if both structure variables are of same type. You don't need to manually assign each members.
Then the structure variable p is passed to displayData() function, which displays the information.

Tag : C++ Arrays, Multidimensional Arrays, C++ Function and Array, C++ String


Selain Sebagai Penyedia Panduan Belajar Database dan Tutorial Pemrograman, Kami Juga Membagikan Kumpulan Source Code Program Aplikasi dan Ebook Pemrograman Terlengkap yang Bisa Anda Dapatkan Secara Gratis di Halaman :

Kategori artikel yang terkait dengan C Plus Plus

C Plus Plus, Panduan C++, Belajar C++ Lengkap, Tutorial C++, Belajar C++ Pdf, Pemrograman C++
Untuk mendapat notifikasi artikel terbaru, masukkan e-mail anda disini :
Selanjutnya cek e-mail untuk verifikasi.
Kincai Coding Panduan Belajar Database dan Tutorial Pemrograman Terlengkap di Indonesia. Dapatkan Kumpulan Source Code Program Aplikasi dan Ebook Secara Gratis. Ikuti kami di Facebook, Instagram & Sosial Media Lainnya.