Загрузка страницы

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
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
30 августа 2020 г. 16:30:03
00:07:53
Другие видео канала
Cache Mapping Techniques 3 || Computer Organisation and Architecture ||Detailed ExplanationCache Mapping Techniques 3 || Computer Organisation and Architecture ||Detailed ExplanationStatic And Dynamic Memory Allocation || Data Structures and Algorithms || Saral Bhasha meStatic And Dynamic Memory Allocation || Data Structures and Algorithms || Saral Bhasha meTime And Space Complexity || Data Structures and Algorithm || Saral Bhasha meTime And Space Complexity || Data Structures and Algorithm || Saral Bhasha meCache Mapping Techniques || Computer Organisation and Architecture ||Detailed ExplanationCache Mapping Techniques || Computer Organisation and Architecture ||Detailed ExplanationSandeep Maheshwari Inspires || Powerful Inspirational SpeechSandeep Maheshwari Inspires || Powerful Inspirational SpeechInstruction cycle State Transition Diagram ||Computer Organisation and Architecture||Saral Bhasha meInstruction cycle State Transition Diagram ||Computer Organisation and Architecture||Saral Bhasha meFloating Point Representation || Computer Architecture || HindiFloating Point Representation || Computer Architecture || HindiArrays Basics || Data Structures and Algorithms || Saral Bhasha meArrays Basics || Data Structures and Algorithms || Saral Bhasha meShri Ram Chandra Kripalu Bhajman ll by THE Gayak Brahmin ll Shri Ram Bhumi Pujan SpecialShri Ram Chandra Kripalu Bhajman ll by THE Gayak Brahmin ll Shri Ram Bhumi Pujan SpecialHardwired Control Unit | Computer Organization and Architecture | Saral Bhasha meHardwired Control Unit | Computer Organization and Architecture | Saral Bhasha meIntroduction to Computer Organisation and Architecture || Computer Organisation and ArchitectureIntroduction to Computer Organisation and Architecture || Computer Organisation and ArchitectureFloating point representation ||  Computer Architecture || HindiFloating point representation || Computer Architecture || HindiWhat every Indian Student must do || Don't miss these tips by Sir Walter lewin || Physics wallahWhat every Indian Student must do || Don't miss these tips by Sir Walter lewin || Physics wallahPointers to a Structure || Data Structures and Algorithms || Saral Bhasha mePointers to a Structure || Data Structures and Algorithms || Saral Bhasha meCache Mapping Techniques 2 || Computer Organisation and Architecture ||Detailed ExplanationCache Mapping Techniques 2 || Computer Organisation and Architecture ||Detailed ExplanationWho is a Teacher || unke kartavya kya haiWho is a Teacher || unke kartavya kya haiStructures in C || Data Structure and Algorithms || Saral Bhasha meStructures in C || Data Structure and Algorithms || Saral Bhasha meIntroduction to Data Structures || Data Structure and Algorithms || Saral Bhasha me!!!Introduction to Data Structures || Data Structure and Algorithms || Saral Bhasha me!!!Addressing modes | Computer Organisation and architectureAddressing modes | Computer Organisation and architectureYou Didn't come this far to only come this far!!!Ultimate motivation for jee,neet students!You Didn't come this far to only come this far!!!Ultimate motivation for jee,neet students!
Яндекс.Метрика