Marking Guide to the CA in Programming 1 Level 1 SWE/CSN/CWD
1) What is a function in C Programming? What are it's benefits?
2) What is the difference between a function declaration and a function call?
3) a) Write a function to add two Integer number.
b) Using this function, write a main program in C language to add the number 17 and 23
1. Function in C Programming
What is a function?
A function in C programming is a block of code that performs a specific task. It can take arguments,
execute a set of statements, and return a value.
Benefits of functions:
- Code reusability : Functions can be called multiple times from different parts of a program, reducing
code duplication.
- Modularity : Functions help break down a large program into smaller, manageable modules.
- Easier maintenance : Functions make it easier to modify or debug specific parts of a program.
2. Function Declaration vs Function Call
Function declaration:
A function declaration, also known as a function prototype, specifies the function's name, return type,
and parameters. It informs the compiler about the function's existence and signature.
Function call:
A function call invokes the function, passing arguments and executing the function's code.
3. Example Function and Main Program
a) Function to add two integers
int add(int a, int b)
{
return a + b;
}
b) Main program to add 17 and 23
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int num1 = 17;
int num2 = 23;
int result = add(num1, num2);
printf("The sum of %d and %d is: %d\n", num1, num2, result);
return 0;
}
In this example, the `add` function takes two integers as arguments and returns their sum. The
`main` function calls `add` with the numbers 17 and 23, storing the result in the `result` variable
and printing it to the console.
4) Write a program to store 5 elements in an Array and prints them.
5) Write a C program to find the sum of all the elements of an array of Size 5
4. Program to Store and Print Array Elements
Here's a simple C program to store 5 elements in an array and print them:
#include <stdio.h>
int main() {
int arr[5]; // Declare an array of size 5
// Store elements in the array
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// Print array elements
printf("Array elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
5. Program to Find the Sum of Array Elements
Here's a C program to find the sum of all elements in an array of size 5:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Initialize an array
int sum = 0; // Variable to store the sum
// Calculate the sum of array elements
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
// Print the sum
printf("Sum of array elements: %d\n", sum);
return 0;
}
In this program, we initialize an array with 5 elements and use a `for` loop to iterate through each
element, adding it to the `sum` variable. Finally, we print the sum of the array elements.