Artikel Terkait Tutorial C
Structure variables can be passed to a function and returned from a function in similar way as normal arguments:
The output of this program is same as program above.
In this program, the structure variable p of type structure person is defined under
Then the structure variable p is passed to
Tag : C++ Arrays, Multidimensional Arrays, C++ Function and Array, C++ String
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;
}
OutputEnter Full name: Bill JobsIn this program, user is asked to enter the data for the members of a structure variable inside
Enter age: 55
Enter salary: 34233.4
Displaying Information.
Name: Bill Jobs
Age: 55
Salary: 34233.4
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;
}
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 :
Rekomendasi Web Hosting
- 20rb perbulan. Diskon hingga 40% kode kupon: MCP Daftar disini (apache).
- 10rb perbulan. Diskon hingga 75% kode kupon: MCP Daftar disini (litespeed).
- 10rb perbulan. Diskon hingga 70% kode kupon: aff-MCP Daftar disini (apache).