Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

C++ language |Structure with function

Posted on October 18, 2017September 28, 2019

Structure with function in C++ language

In this tutorial, we will discuss the concept of Structure with function in C++ language

a structure variable can be passed to a function  as an argument

There are two way to pass a function as argument

  • passing  to a function by value
  • passing  to a function by reference


passing to a function by value

In this case, The  structure object is passed as a function argument to the definition of function. “Structure object” represents the structure’s members and their values.

Program 1

C++ language |Structure with function
Example
In this program, first, variables name, age, marks are declared inside the student structure
Then the user is asked to enter the name, age and marks of a student inside the main() function
Finally structure variable s is to passed to a function

When the above program is executed, it produces the following results
 
Enter student full name :
Jhon mackal
Enter student s age :
34
Enter student s marks:
88

Display student details
...........................................
student name : Jhon mackal
student age : 34
student marks : 88


Passing to function by reference

In this case, the address(reference) of the structure variables are passed as a function argument to the definition of the function.

Program 2

#include <iostream>
#include <conio.h>
using namespace std;
struct student{
int rgNo;
char gender;
int age;
};
void display (student s);
//Declaration of display function with normal reference
void show (student *s);
//Declaration of show function with pointer reference

int main()
{
    student anil={1122,'m',26};
    display(anil);//Call the display function
    show(&anil);//Call the show function
    getch();
    return 0;
}
void display(student s){
    cout<<"Result using display function"<<endl;
//Definition of display function with normal variable
cout<<s.rgNo<<endl;
cout<<s.gender<<endl;
cout<<s.age<<endl<<endl;
}

void show(student *s){
    cout<<"Result using show function"<<endl;
//Definition of display function with pointer variable
cout<<s->rgNo<<endl;
cout<<s->gender<<endl;
cout<<s->age<<endl;
}

 

When the above program is executed, it produces the following results.

This output received pass by value

result using display function
1122
m
26

This output received pass by reference

result using show function
1122
m
26

Related post

Structure in C Language                             Structure in Cpp Language
Structure with function in C Language 
 Structure with pointer in Cpp Language                 Structure with pointer
 
Nested function  in C        
 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes