Passing structure to a function pass-by-value – Computer Sir Ki Class


Lost your password?

Don't have an account ?
Register (It's FREE) ×


Code Learning #CPP#2969 siteicon   siteicon   siteicon  

Passing structure to a function pass-by-value

A program to demonstrate passing of structure to a function using pass by value method,

Learning Objectives

  • Learning to pass structure to a function using the pass by value in the argument of the function.

Source Code

TC++ #2969


Source Code

Run Output

ItemID  Qty     UP      Price
1 7 25 175
1 3 15 45

Code Understanding

struct Sale { int ItemID; int qty; float uprice;};
Structure Sale is declared with some predefined variables.

float price(Sale s) { return s.qty*s.uprice; }
This is the function where the whole structure is passed as its argument. This function returns the price based on multiplication of s.qty and s.uprice. The Sale s argument creates a local instance of structure which can be used in this function body only. This is pass by value technique.

Sale s1={1,7,25.0},s2={1,3,15.0};
Two instances s1 and s2 are declared and initialised with the given values. The value type and order should match the declaration given in the structure Sale.

These lines to print heading lines before the data.

This line prints the data for instance s1 of the structure. Notice the function call price(s1) where the structure is directly passed to the function

This line is for another instance data printing.


  • Structure passing by value works exactly like single variable passing, but it has a clear advantage of passing many variables of different data types together.
  • Although structure instance passed as argument is local to the function only, it is still recommended to use a different name from the other instance names. This is advisable as mostly structure have publicly available instances.


Suggested Filename(s): struc2fnv.cpp


sunmitra| Created: 24-Jan-2018 | Updated: 24-Jan-2018|

Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning 2D Array Memory Addressing Display Using IO Manipulation Display Using C Formatting Tricks User Defined Data Types Enumerated Types Preprocessor Directives And Macros Exception Handling Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Function Overloading Special Cases Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constant Functions Nesting of Functions Class Members Scope Resolution Static Members in a Class Array of Objects Constructor Concepts Default Constructor Parameterized Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Multi-Level Inheritance Class Nesting Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Pointers and Functions Object Pointers This Pointer Linked Lists Stacks Queues