C Programming • Structure and Union
C Programming / Functions and structures

Functions and structures

Notes 4 Structure and Union

Structures and functions can be used together to make a program more organized and reusable. A structure variable can be passed to a function so that the function can access or process the related data stored in the structure.

Notes

Structures and functions can be used together to make a program more organized and reusable. A structure variable can be passed to a function so that the function can access or process the related data stored in the structure.

💡 Example

A Student structure can be passed to a function named displayStudent() to display the student's details.

Passing a Structure to a Function

A structure variable can be passed to a function just like other variables. The called function receives a copy of the structure and can use its members.

💡 Example
displayStudent(s1);

Here, s1 is a structure variable passed to the function displayStudent().

Function Parameter for a Structure

The parameter of a function can be declared using the structure type. This allows the function to receive a complete structure record.

💡 Example
void displayStudent(struct Student s) { printf("%d", s.rollNo); }

The function receives a struct Student variable as its parameter and accesses its member using the dot operator.

Accessing Structure Members Inside a Function

Once a structure variable is available inside a function, its members can be accessed using the dot operator in the same way as in main().

💡 Example
s.rollNo s.percentage

These expressions access individual members of the structure variable s.

Why Use Structures with Functions?

Combining structures and functions helps divide a program into smaller tasks. One function can accept a record, process it, or display it, while other parts of the program remain unchanged.

💡 Example

In a student management program, one function can read student details and another function can display those details.

Structure and Function Flow

Structure Variable ↓ Function Call ↓ Structure Parameter ↓ Process / Display Members ↓ Result

Practical Example

Problem Statement

Write a C program to create a student structure and pass a structure variable to a function that displays the student's roll number and percentage.

Learning Outcomes

  • Pass a structure variable to a function.
  • Define a function that accepts a structure as a parameter.
  • Access structure members inside a function.

Hint

Define a Student structure, create a structure variable, and pass it to a function named displayStudent().

Theory

A structure can be passed to a function as an argument. When a structure is passed by value, the function receives a copy of the structure and can access its members using the dot operator.

Program

#include <stdio.h> struct Student { int rollNo; float percentage; }; void displayStudent(struct Student s) { // display the members received from the structure printf("Roll Number = %d ", s.rollNo); printf("Percentage = %.1f ", s.percentage); } int main() { struct Student s1; printf("Enter roll number: "); scanf("%d", &s1.rollNo); printf("Enter percentage: "); scanf("%f", &s1.percentage); // pass the complete structure to the function displayStudent(s1); return 0; }

Expected Output

Enter roll number: 101

Enter percentage: 82.5

Roll Number = 101

Percentage = 82.5

Note

In this program, s1 is passed to displayStudent(). The function receives the structure and accesses its members using s.rollNo and s.percentage.

Returning a Structure from a Function

A function can also return a complete structure to the calling function. The return type of the function is declared as the corresponding structure type.

💡 Example
struct Student createStudent() { struct Student s = {101, 82.5}; return s; }

The function creates a Student structure and returns it to the calling function.

Quick Revision

Concept Remember
Structure as Argument A structure variable can be passed to a function.
Structure Parameter Function parameter can be declared as a structure type.
Member Access Use the dot operator inside the function.
Return Structure A function can return a complete structure.

Important Exam Questions

Short Answer Questions

  1. How can a structure variable be passed to a function?
  2. How is a structure parameter declared?
  3. How are structure members accessed inside a function?
  4. Can a function return a structure?
  5. Why are structures used with functions?

Long Answer Questions

  1. Explain how structures can be passed to functions with a suitable example.
  2. Write a C program to pass a structure variable to a function and display its members.
  3. Explain how a function can return a structure.
🎥 Recommended Learning

Watch a beginner-friendly explanation of passing structures to functions in C.

▶ Watch: Structures and Functions in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for structures and functions will be provided here.

🧠 Mind Map

Use the mind map for quick revision of structure arguments, parameters, member access and return values.