Structures and Functions|| Data Structures and Algorithms || Saral Bhasha me
Structures and Functions|| Data Structures and Algorithms || Saral Bhasha me
#structure #functions #datastructures #Clanguage #C++ #aktu #akturesults #ugc #algorithms
Structure and Function in C++
BY CHAITANYA SINGH | FILED UNDER: LEARN C++
In this previous tutorial we learnt about structures, the compound data type that groups different types of variables. In this tutorial, we will learn how to pass structures as an argument to the function and how to return the structure from the function.
How to pass structure as an argument to function
Here we have a function printStudentInfo() which takes structure Student as an argument and prints the details of student using structure varaible. The important point to note here is that you should always declare the structure before function declarations, otherwise you will get compilation error.
#include iostream
using namespace std;
struct Student{
char stuName[30];
int stuRollNo;
int stuAge;
};
void printStudentInfo(Student);
int main(){
Student s;
cout"Enter Student Name: ";
cin.getline(s.stuName, 30);
coutEnter Student Roll No: ";
cins.stuRollNo;
cout"Enter Student Age: ";
cins.stuAge;
printStudentInfo(s);
return 0;
}
void printStudentInfo(Student s){
coutStudent Record:endl;
cout"Name: "s.stuNameendl;
cout"Roll No: "s.stuRollNoendl;
cout"Age: "s.stuAge;
}
Output:
Enter Student Name: Rick
Enter Student Roll No: 666123
Enter Student Age: 19
Student Record:
Name: Rick
Roll No: 666123
Age: 19
How to return the Structure from a Function
In this example we have two functions one gets the values from user, assign them to structure members and returns the structure and the other function takes that structure as argument and print the details.
#include iostream
using namespace std;
struct Student{
char stuName[30];
int stuRollNo;
int stuAge;
};
Student getStudentInfo();
void printStudentInfo(Student);
int main(){
Student s;
s = getStudentInfo();
printStudentInfo(s);
return 0;
}
/* This function prompt the user to input student
* details, stores them in structure members
* and returns the structure
*/
Student getStudentInfo(){
Student s;
cout"Enter Student Name: ";
cin.getline(s.stuName, 30);
coutEnter Student Roll No: ";
cins.stuRollNo;
coutEnter Student Age: ";
cins.stuAge;
return s;
}
void printStudentInfo(Student s){
coutStudent Record:"endl;
cout"Name: "s.stuNameendl;
coutRoll No: "s.stuRollNendl;
cout"Age: "s.stuAge;
}
Output:
Enter Student Name: Tyrion lannister
Enter Student Roll No: 333901
Enter Student Age: 39
Student Record:
Name: Tyrion lannister
Roll No: 333901
Age: 39
Видео Structures and Functions|| Data Structures and Algorithms || Saral Bhasha me канала Codemasters
#structure #functions #datastructures #Clanguage #C++ #aktu #akturesults #ugc #algorithms
Structure and Function in C++
BY CHAITANYA SINGH | FILED UNDER: LEARN C++
In this previous tutorial we learnt about structures, the compound data type that groups different types of variables. In this tutorial, we will learn how to pass structures as an argument to the function and how to return the structure from the function.
How to pass structure as an argument to function
Here we have a function printStudentInfo() which takes structure Student as an argument and prints the details of student using structure varaible. The important point to note here is that you should always declare the structure before function declarations, otherwise you will get compilation error.
#include iostream
using namespace std;
struct Student{
char stuName[30];
int stuRollNo;
int stuAge;
};
void printStudentInfo(Student);
int main(){
Student s;
cout"Enter Student Name: ";
cin.getline(s.stuName, 30);
coutEnter Student Roll No: ";
cins.stuRollNo;
cout"Enter Student Age: ";
cins.stuAge;
printStudentInfo(s);
return 0;
}
void printStudentInfo(Student s){
coutStudent Record:endl;
cout"Name: "s.stuNameendl;
cout"Roll No: "s.stuRollNoendl;
cout"Age: "s.stuAge;
}
Output:
Enter Student Name: Rick
Enter Student Roll No: 666123
Enter Student Age: 19
Student Record:
Name: Rick
Roll No: 666123
Age: 19
How to return the Structure from a Function
In this example we have two functions one gets the values from user, assign them to structure members and returns the structure and the other function takes that structure as argument and print the details.
#include iostream
using namespace std;
struct Student{
char stuName[30];
int stuRollNo;
int stuAge;
};
Student getStudentInfo();
void printStudentInfo(Student);
int main(){
Student s;
s = getStudentInfo();
printStudentInfo(s);
return 0;
}
/* This function prompt the user to input student
* details, stores them in structure members
* and returns the structure
*/
Student getStudentInfo(){
Student s;
cout"Enter Student Name: ";
cin.getline(s.stuName, 30);
coutEnter Student Roll No: ";
cins.stuRollNo;
coutEnter Student Age: ";
cins.stuAge;
return s;
}
void printStudentInfo(Student s){
coutStudent Record:"endl;
cout"Name: "s.stuNameendl;
coutRoll No: "s.stuRollNendl;
cout"Age: "s.stuAge;
}
Output:
Enter Student Name: Tyrion lannister
Enter Student Roll No: 333901
Enter Student Age: 39
Student Record:
Name: Tyrion lannister
Roll No: 333901
Age: 39
Видео Structures and Functions|| Data Structures and Algorithms || Saral Bhasha me канала Codemasters
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![Cache Mapping Techniques 3 || Computer Organisation and Architecture ||Detailed Explanation](https://i.ytimg.com/vi/4jzC1owhbEw/default.jpg)
![Static And Dynamic Memory Allocation || Data Structures and Algorithms || Saral Bhasha me](https://i.ytimg.com/vi/wdtMD6jWisI/default.jpg)
![Time And Space Complexity || Data Structures and Algorithm || Saral Bhasha me](https://i.ytimg.com/vi/C_ZpLHnH8zc/default.jpg)
![Cache Mapping Techniques || Computer Organisation and Architecture ||Detailed Explanation](https://i.ytimg.com/vi/sPZwmzRLh7Q/default.jpg)
![Sandeep Maheshwari Inspires || Powerful Inspirational Speech](https://i.ytimg.com/vi/WjTiMawTVmM/default.jpg)
![Instruction cycle State Transition Diagram ||Computer Organisation and Architecture||Saral Bhasha me](https://i.ytimg.com/vi/miqZ5T_ialk/default.jpg)
![Floating Point Representation || Computer Architecture || Hindi](https://i.ytimg.com/vi/rAnZ0ulXytU/default.jpg)
![Arrays Basics || Data Structures and Algorithms || Saral Bhasha me](https://i.ytimg.com/vi/FeE4rsc1DS4/default.jpg)
![Shri Ram Chandra Kripalu Bhajman ll by THE Gayak Brahmin ll Shri Ram Bhumi Pujan Special](https://i.ytimg.com/vi/TTvx5DRvXWc/default.jpg)
![Hardwired Control Unit | Computer Organization and Architecture | Saral Bhasha me](https://i.ytimg.com/vi/LdqwuI4W488/default.jpg)
![Introduction to Computer Organisation and Architecture || Computer Organisation and Architecture](https://i.ytimg.com/vi/xi4vJMnzonI/default.jpg)
![Floating point representation || Computer Architecture || Hindi](https://i.ytimg.com/vi/BSHy0JUDGtU/default.jpg)
![What every Indian Student must do || Don't miss these tips by Sir Walter lewin || Physics wallah](https://i.ytimg.com/vi/FZJrdzWxSdY/default.jpg)
![Pointers to a Structure || Data Structures and Algorithms || Saral Bhasha me](https://i.ytimg.com/vi/MC3wXzpplps/default.jpg)
![Cache Mapping Techniques 2 || Computer Organisation and Architecture ||Detailed Explanation](https://i.ytimg.com/vi/Jp2vTU2qeiM/default.jpg)
![Who is a Teacher || unke kartavya kya hai](https://i.ytimg.com/vi/NtyWS3x39S4/default.jpg)
![Structures in C || Data Structure and Algorithms || Saral Bhasha me](https://i.ytimg.com/vi/kMGKJeVfod4/default.jpg)
![Introduction to Data Structures || Data Structure and Algorithms || Saral Bhasha me!!!](https://i.ytimg.com/vi/_yEf2S_Tx6k/default.jpg)
![Addressing modes | Computer Organisation and architecture](https://i.ytimg.com/vi/mYm9pqum6_o/default.jpg)
![You Didn't come this far to only come this far!!!Ultimate motivation for jee,neet students!](https://i.ytimg.com/vi/hgoE8zbXCwg/default.jpg)