Week 10: Functions & Advanced C Concepts
Q1. Call by Value
Concept: In call by value, the function receives a copy of the variable. Changes inside the function do not affect the original variable.
Program:
#include <stdio.h>
void change(int num); // Function prototype
int main() {
int x = 100;
printf("Before function call x = %d\n", x);
change(x);
printf("After function call x = %d\n", x);
return 0;
}
void change(int num) {
printf("Before adding inside function num = %d\n", num);
num += 100;
printf("After adding inside function num = %d\n", num);
}
Output Example:
Before function call x = 100
Before adding inside function num = 100
After adding inside function num = 200
After function call x = 100
Q2. Call by Reference
Concept: In call by reference, the function receives the address of the variable, so changes inside the function modify the original variable.
Program:
#include <stdio.h>
void change(int *num); // Function prototype
int main() {
int x = 100;
printf("Before function call x = %d\n", x);
change(&x);
printf("After function call x = %d\n", x);
return 0;
}
void change(int *num) {
printf("Before adding inside function num = %d\n", *num);
*num += 100;
printf("After adding inside function num = %d\n", *num);
}
Output Example:
Before function call x = 100
Before adding inside function num = 100
After adding inside function num = 200
After function call x = 200
Q3. Sum of Elements in an Array Using Function
Program:
#include <stdio.h>
float calculateSum(float num[], int size) {
float sum = 0.0;
for(int i = 0; i < size; i++)
sum += num[i];
return sum;
}
int main() {
int size;
printf("Enter number of elements: ");
scanf("%d", &size);
float num[size];
printf("Enter elements: ");
for(int i = 0; i < size; i++)
scanf("%f", &num[i]);
float result = calculateSum(num, size);
printf("Sum = %.2f\n", result);
return 0;
}
Q4. Sort Array Using Selection Sort Function
Program:
#include <stdio.h>
void sort(int a[], int len) {
for(int i = 0; i < len - 1; i++) {
int min_index = i;
for(int j = i + 1; j < len; j++) {
if(a[j] < a[min_index])
min_index = j;
}
int temp = a[i];
a[i] = a[min_index];
a[min_index] = temp;
}
}
int main() {
int len;
printf("Enter number of elements: ");
scanf("%d", &len);
int a[len];
printf("Enter elements: ");
for(int i = 0; i < len; i++)
scanf("%d", &a[i]);
sort(a, len);
printf("Sorted array: ");
for(int i = 0; i < len; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
Q5. Check Palindrome String Using Function
Program:
#include <stdio.h>
void palindrome(char str[]) {
int left = 0, right = 0, flag = 1;
// Find string length
while(str[right] != '\0') right++;
right--; // Last index
// Check characters from both ends
while(left < right) {
if(str[left] != str[right]) {
flag = 0;
break;
}
left++;
right--;
}
if(flag)
printf("%s is a palindrome\n", str);
else
printf("%s is not a palindrome\n", str);
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
palindrome(str);
return 0;
}
Example:
Input: radar
Output: radar is a palindrome
Key Points:
- Call by Value: Only copies variable; original value unchanged.
- Call by Reference: Pass address; function modifies original value.
- Array Handling: Arrays are passed by reference by default in C.
- Sorting: Selection sort repeatedly finds the minimum and swaps.
- Palindrome Check: Compare characters from start and end until the middle.