Unit 5 Q & A
Unit 5 Q & A
What is a Function?
Example of a Function
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction(); // call the function
return 0;
}
1. Code Reusability: Functions allow you to write code once and reuse it multiple
times. Instead of repeating the same code, you can simply call the function whenever
needed. This reduces the overall size of your code and makes it more efficient.
2. Modularity: Functions break down a complex task into smaller, manageable pieces.
Each function can focus on a specific subtask, making the overall system easier to
understand and maintain.
3. Maintainability: Functions improve the maintainability of your code. If a specific
behavior needs to change, you can update it in just one place (the function), rather
than modifying the same code in multiple locations.
4. Readability: Functions help organize the code logically. It’s easier for other
developers (or even yourself) to understand what the code does by looking at the
function names, especially when they are descriptive (e.g., calculate_area instead of a
long block of code doing the calculation).
5. Debugging: Functions make debugging easier. If there is an issue, you can isolate the
problem to a particular function rather than sifting through a massive chunk of code.
6. Scoping: Functions allow you to define local variables that are used only within the
function, which avoids potential conflicts with other parts of the program.
Example of Need:
Imagine you are writing a program to calculate the area of multiple shapes (circle, rectangle,
square, etc.). Without functions, you would write the code to calculate the area repeatedly for
each shape. With functions, you can create separate functions for each shape's area
calculation and call them whenever needed.
1. Function Call
A function call is the process of invoking or executing a function that has been defined
elsewhere in the program. It tells the program to jump to the function's definition, execute the
statements inside the function, and return the result (if any).
#include <stdio.h>
int main() {
int sum = add(3, 4); // Function call
printf("Sum: %d\n", sum); // Output: Sum: 7
return 0;
}
In this example, add(3, 4) is the function call that invokes the add function, passing 3 and 4
as arguments.
2. Function Definition
A function definition is where the actual body of the function is written. It contains the code
that specifies what the function does when it is called.
Return Type: Specifies what type of value the function returns (if any). If it doesn't
return anything, it uses void.
Function Name: The identifier that is used to call the function.
Parameters (optional): Input values the function will work with. If the function
doesn't require any parameters, it can take void in the parentheses.
Function Body: A block of code that performs the task and returns the result (if any).
int main() {
int result = multiply(2, 5); // Calling the function
printf("Result: %d\n", result); // Output: Result: 10
return 0;
}
In this example:
int multiply(int a, int b) is the function definition where a and b are parameters, and
the function returns the result of multiplying a and b.
The code inside {} is the body of the function that executes when it is called.
A function declaration (also called a function prototype) tells the compiler about the
function's name, return type, and parameters before the function is actually defined. It
provides information to the compiler so that it knows how to handle the function when it is
called, even if the full definition comes later in the code.
The function declaration is typically placed before the main function or at the top of the file
to give the compiler enough information to process function calls.
Return Type
Function Name
Parameter Types (parameter names are optional in the declaration)
#include <stdio.h>
int main() {
int result = subtract(10, 4); // Function call
printf("Result: %d\n", result); // Output: Result: 6
return 0;
}
// Function definition
int subtract(int a, int b) {
return a - b;
}
In this example:
The function declaration int subtract(int a, int b); informs the compiler that a
function named subtract exists, returns an integer, and takes two integer parameters.
The function definition later provides the actual implementation of what the subtract
function does (subtracting b from a).
Function Recursion in C
Let's take the factorial of a number as an example. The factorial of a number n is the product
of all positive integers less than or equal to n. The formula for factorial is:
factorial(n) = n * factorial(n-1)
Base case: factorial(0) = 1
#include <stdio.h>
int main() {
int number = 5;
printf("Factorial of %d is %d\n", number, factorial(number)); // Output: 120
return 0;
}
Explanation:
1. Function Definition:
o The function factorial takes an integer n as an argument.
o If n is 0, the function returns 1, which is the base case (factorial(0) = 1).
oOtherwise, the function returns n * factorial(n - 1), which is the recursive call.
The function continues to call itself with smaller values of n until it reaches
the base case.
2. Function Call:
o In the main function, factorial(5) is called, which triggers the series of
recursive calls:
factorial(5) → 5 * factorial(4)
factorial(4) → 4 * factorial(3)
factorial(3) → 3 * factorial(2)
factorial(2) → 2 * factorial(1)
factorial(1) → 1 * factorial(0)
factorial(0) returns 1 (base case).
o After reaching the base case, the values are multiplied and the final result
(120) is returned back through the chain of recursive calls.
Structures in C
A structure in C is a user-defined data type that allows grouping different data types under a
single name. Structures are used to represent a record, where each record can have multiple
attributes, each of which can have a different data type.
In simpler terms, a structure is like a container that can hold different types of data together.
It is very useful when you want to store and organize related data together, such as storing a
person's name, age, and address in a single structure.
Syntax of a Structure
To define a structure, you use the struct keyword, followed by the structure's name and the
members (or fields) inside curly braces {}.
struct StructureName {
data_type member1;
data_type member2;
// More members can be added
};
Example of a Structure
#include <stdio.h>
int main() {
// Creating a variable of type struct Book
struct Book myBook;
return 0;
}
#include <stdio.h>
#include <string.h>
struct Employee {
};
scanf("%d", &e->id);
scanf("%f", &e->salary);
printf("\nEmployee Details:\n");
int main() {
inputEmployeeDetails(&emp);
displayEmployeeDetails(emp);
return 0;