0% found this document useful (0 votes)
33 views5 pages

pps module 4 assignment

Uploaded by

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

pps module 4 assignment

Uploaded by

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

Functions and Advanced Concepts in C

1. Message Passing Methods in C


In C, functions can communicate with each other through parameters. There are two
primary methods of passing messages between functions: Call by Value and Call by
Reference.

Call by Value
In Call by Value, the actual value of the argument is copied into the formal parameter. Any
changes made to the formal parameter do not affect the actual argument.

Example:

void swap(int x, int y) {


int temp = x;
x = y;
y = temp;
}

int main() {
int a = 5, b = 10;
swap(a, b);
printf("%d %d", a, b); // Output: 5 10
return 0;
}

Call by Reference
In Call by Reference, the memory address of the actual argument is passed to the formal
parameter. Any changes made to the formal parameter affect the actual argument.

Example:

void swap(int *x, int *y) {


int temp = *x;
*x = *y;
*y = temp;
}

int main() {
int a = 5, b = 10;
swap(&a, &b);
printf("%d %d", a, b); // Output: 10 5
return 0;
}

2. Dynamic Memory Allocation in C


Dynamic memory allocation in C allows programmers to allocate memory at runtime. This
is useful when the amount of memory required is unknown until the program executes.

malloc()
malloc() allocates a block of memory of specified size.

Example:

int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
printf("%d", *ptr); // Output: 10

calloc()
calloc() allocates multiple blocks of memory of specified size.

Example:

int *ptr;
ptr = (int *)calloc(5, sizeof(int));
ptr[0] = 10;
printf("%d", ptr[0]); // Output: 10

realloc()
realloc() changes the size of a memory block previously allocated.

Example:

int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
ptr = (int *)realloc(ptr, 2 * sizeof(int));
ptr[1] = 20;
printf("%d %d", ptr[0], ptr[1]); // Output: 10 20

free()
free() releases a block of memory previously allocated.

Example:

int *ptr;
ptr = (int *)malloc(sizeof(int));
*ptr = 10;
free(ptr);

3. Recursive Function
A recursive function is a function that calls itself repeatedly until it reaches a base case.

Example: Fibonacci Sequence using Recursive Function

#include <stdio.h>

int fibonacci(int n) {
if (n == 0 || n == 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Fibonacci number: %d", fibonacci(n));
return 0;
}

4. Functions in C
A function in C is a block of code that performs a specific task.

Function Declaration
Function declaration is the process of telling the compiler about the function's name, return
type, and parameters.
Example:

int add(int, int);

Function Definition
Function definition is the actual code that gets executed when the function is called.

Example:

int add(int x, int y) {


return x + y;
}

Function Call
Function call is the process of invoking the function to perform its task.

Example:

int result = add(5, 10);


printf("%d", result); // Output: 15

5. Categorization of Functions in C
Functions in C can be categorized into:

1. Library Functions: Pre-defined functions provided by the C standard library, such as


printf() and scanf().
2. User-Defined Functions: Functions defined by the programmer to perform specific tasks.
3. Recursive Functions: Functions that call themselves repeatedly until they reach a base
case.

Advantages of using functions in C:

1. Modularity: Functions help break down the code into smaller, manageable modules.
2. Reusability: Functions can be reused throughout the program, reducing code duplication.
3. Readability: Functions improve code readability by providing a clear and concise
description of the code's functionality.
4. Maintainability: Functions make it easier to modify and maintain the code.

You might also like