0% found this document useful (0 votes)
7 views9 pages

C Programming Unit4

The document provides an introduction to functions in C programming, explaining their purpose, types, and structure. It details user-defined functions, their categories based on arguments and return values, and the differences between local and global variables. Additionally, it covers structures and unions, including their definitions, syntax, and examples, as well as the concept of an array of structures.

Uploaded by

dbossd183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views9 pages

C Programming Unit4

The document provides an introduction to functions in C programming, explaining their purpose, types, and structure. It details user-defined functions, their categories based on arguments and return values, and the differences between local and global variables. Additionally, it covers structures and unions, including their definitions, syntax, and examples, as well as the concept of an array of structures.

Uploaded by

dbossd183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to Functions

 A function is a block of code designed to perform a specific task.


 Functions make programming modular and reusable by breaking the program into smaller
parts called modules or sub-programs.
 Advantages of Modularization:
o Reusability: Code can be reused multiple times.
o Ease of Debugging: Errors are easier to locate and fix.
o Portability: Reusable code can be shared across projects.

Types of Functions in C

1. Standard Functions (Library Functions): Predefined in libraries (e.g., printf(), scanf(), sqrt(),
etc.).
2. User-Defined Functions (UDFs): Written by the programmer for specific tasks.

Structure of a User-Defined Function

1. Function Declaration (Prototype): Declares the function to the compiler.


2. Function Call: Invokes the function from the main() or another function.
3. Function Definition: Contains the logic or task the function performs.

Steps to Write a Program Using UDFs

1. Function Declaration (Prototype)

Tells the compiler about the function name, return type, and parameters.

Syntax:

return_type function_name(parameter_list);

Examples:

int add();
int multiply(int a, int b);

2. Function Call

Used to invoke the function.

Syntax:

function_name(arguments);
Examples:

add();
multiply(x, y);

3. Function Definition

Contains the actual logic or task of the function.

Syntax:

return_type function_name(parameter_list) {
// function body
return value;
}

Examples:

int add(int x, int y) {


return x + y;
}

Categories of User-Defined Functions

Based on arguments and return values, User-Defined Functions are classified into four categories:

1. Function with No Arguments and No Return Value

The function neither takes arguments nor returns a value.

Example:

void greet() {
printf("Hello, World!");
}

2. Function with No Arguments but Returns a Value

The function does not take arguments but returns a value.

Example:

int getNumber() {
return 42;
}
3. Function with Arguments but No Return Value

The function takes arguments but does not return a value.

Example:

void printSum(int a, int b) {


printf("Sum: %d", a + b);
}

4. Function with Arguments and Returns a Value

The function takes arguments and returns a value.

Example:

int multiply(int a, int b) {


return a * b;
}

Arguments and Parameters

 Arguments: Variables passed to a function during a function call.


 Parameters: Variables used in a function definition to receive arguments.
 Key Points:
1. The number of arguments and parameters must match.
2. Arguments and parameters must have the same order and data type.

Local and Global Variables

1. Local Variables

Declared inside a function.

Accessible only within the function where they are declared.

Destroyed after the function execution ends.

Example:

void localExample() {
int x = 10; // Local variable
printf("%d", x);
}
2. Global Variables

Declared outside all functions.

Accessible by all functions in the program.

Lifetime is until the program ends.

Example:

int x = 10; // Global variable

void display() {
printf("%d", x);
}

1. Function with No Arguments and No Return Value

 The function does not take any arguments and does not return any value.
 Used for simple tasks like displaying messages or performing operations without input or
output.

Example:

#include <stdio.h>
// Function definition
void displayMessage() {
printf("Hello! Welcome to C Programming.\n");
}
int main() {
displayMessage(); // Function call
return 0;
}

Output:

Hello! Welcome to C Programming.

2. Function with No Arguments but Returns a Value

 The function does not take any arguments but returns a value to the calling function.

Example:

#include <stdio.h>
// Function definition
int getNumber() {
return 42; // Returning a constant value
}
int main() {
int number = getNumber(); // Function call
printf("The number is: %d\n", number);
return 0;
}

Output:

The number is: 42

3. Function with Arguments but No Return Value

 The function takes arguments (inputs) but does not return any value.

Example:

#include <stdio.h>
// Function definition
void displaySum(int a, int b) {
printf("The sum of %d and %d is: %d\n", a, b, a + b);
}
int main() {
int x = 10, y = 20;
displaySum(x, y); // Function call with arguments
return 0;
}

Output:

The sum of 10 and 20 is: 30

4. Function with Arguments and Returns a Value

 The function takes arguments and also returns a value to the calling function.

Example:

#include <stdio.h>
// Function definition
int multiply(int a, int b) {
return a * b; // Returning the product
}

int main() {
int x = 5, y = 6;
int result = multiply(x, y); // Function call with arguments
printf("The product of %d and %d is: %d\n", x, y, result);
return 0;
}

Output:

The product of 5 and 6 is: 30

Structures and Unions:

Structure

 Definition: A collection of one or more variables (of the same or different types) grouped
together under a single name.
 Usage: Helps to represent complex data meaningfully.

Syntax for Structure

struct structure_name {
data_type member1;
data_type member2;
...
};

Example: Employee Structure

struct employee {
int id_no;
char name[15];
char designation[10];
float salary;
};
// Declaring structure variables
struct employee emp1, emp2;
// Accessing members
emp1.id_no = 101;
strcpy(emp1.name, "Alice");
strcpy(emp1.designation, "Manager");
emp1.salary = 50000.0;
Union

 Definition: A collection of variables (of the same or different types) that share the same
memory location.
 Usage: Efficient for memory usage when only one variable needs to be active at a time.

Syntax for Union

union union_name {
data_type member1;
data_type member2;
...
};

Example: Number Union

union number {
int n1;
float n2;
};
// Declaring union variable
union number num;
// Accessing members
num.n1 = 10; // Assigning to `n1`
printf("n1: %d\n", num.n1);
// Assigning to `n2` (overwrites `n1`)
num.n2 = 5.5;
printf("n2: %.2f\n", num.n2);

Key Differences Between Structure and Union

Feature Structure Union


Keyword Declared using struct. Declared using union.
Separate memory for each All members share the same memory
Memory Allocation member. location (size of the largest member).
All members can be accessed Only one member can be accessed at a
Accessing Members simultaneously. time.

Size Size is the sum of all members. Size equals the largest member.
All members can be initialized
Initialization simultaneously. Only the first member can be initialized.
Altering one member does not Altering one member overwrites
Value Alteration affect others. others.
Examples for Structures and Unions

Example 1: Student Information Using Structure

#include <stdio.h>
#include <string.h>
struct student {
int reg_no;
char name[20];
char branch[10];
int semester;
float total_marks;
};
int main() {
struct student s1;
// Assigning values
s1.reg_no = 101;
strcpy(s1.name, "John");
strcpy(s1.branch, "CSE");
s1.semester = 3;
s1.total_marks = 88.5;
// Accessing and displaying members
printf("Student Info:\n");
printf("Reg No: %d\n", s1.reg_no);
printf("Name: %s\n", s1.name);
printf("Branch: %s\n", s1.branch);
printf("Semester: %d\n", s1.semester);
printf("Total Marks: %.2f\n", s1.total_marks);
return 0;
}

Example 2: Person Information Using Union

#include <stdio.h>
#include <string.h>
union person {
char name[20];
int age;
float height;
};
int main() {
union person p1;
// Assigning and displaying name
strcpy(p1.name, "Alice");
printf("Name: %s\n", p1.name);
// Assigning and displaying age (overwrites name)
p1.age = 25;
printf("Age: %d\n", p1.age);
// Assigning and displaying height (overwrites age)
p1.height = 5.6;
printf("Height: %.1f\n", p1.height);
return 0;
}

Array of Structures

Example: Student Marks

#include <stdio.h>
struct marks {
int reg_no;
float subject[3]; // Array within structure
};
int main() {
struct marks student[3]; // Array of structures
// Assigning values
student[0].reg_no = 1;
student[0].subject[0] = 85.0;
student[0].subject[1] = 90.0;
student[0].subject[2] = 88.0;
// Accessing values
printf("Student 1:\n");
printf("Reg No: %d\n", student[0].reg_no);
printf("Subject 1: %.1f\n", student[0].subject[0]);
printf("Subject 2: %.1f\n", student[0].subject[1]);
printf("Subject 3: %.1f\n", student[0].subject[2]);
return 0;
}

You might also like