0% found this document useful (0 votes)
0 views

Unit 5 Q & A

The document explains the concept of functions in programming, detailing their structure, need, and examples of function calls, definitions, and declarations. It also covers recursion, providing an example of calculating factorials, and introduces structures in C, including their syntax and initialization. Lastly, it presents a simple program to create an employee database using structures.

Uploaded by

surajandhale1600
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)
0 views

Unit 5 Q & A

The document explains the concept of functions in programming, detailing their structure, need, and examples of function calls, definitions, and declarations. It also covers recursion, providing an example of calculating factorials, and introduces structures in C, including their syntax and initialization. Lastly, it presents a simple program to create an employee database using structures.

Uploaded by

surajandhale1600
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/ 7

FPL Unit 5 Q & A

Q.1 Explain what is function and need of function

What is a Function?

A function in programming is a block of code designed to perform a specific task. It is a self-


contained module that can take inputs, process them, and return an output. Functions are used
to organize code, reduce repetition, and make the program easier to read and maintain.

A function generally consists of the following parts:

1. Function Name: The identifier used to refer to the function.


2. Parameters (optional): Values passed into the function when it's called. They
provide input to the function.
3. Function Body: The block of code that defines what the function does.
4. Return Statement (optional): The value that the function outputs after processing
the input. Not all functions need to return a value.

Example of a Function
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction(); // call the function
return 0;
}

Need for Functions

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.

Q.2 Explain function call ,definition and function declaration

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).

 When a function is called, control of the program is transferred to that function.


 Once the function has completed its task, it may return a value to the caller (if the
function is designed to return something), and then control returns to the calling code.

Example of Function Call:

#include <stdio.h>

int add(int a, int b) {


return a + b; // Function definition
}

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.

A function definition consists of:

 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).

Example of Function Definition


#include <stdio.h>

int multiply(int a, int b) { // Function definition


return a * b;
}

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.

3. Function Declaration (Prototype)

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.

A function declaration consists of:

 Return Type
 Function Name
 Parameter Types (parameter names are optional in the declaration)

Example of Function Declaration

#include <stdio.h>

// Function declaration (prototype)


int subtract(int a, int b);

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).

Q.3 what is function recursion with example.

Function Recursion in C

Recursion is a programming technique where a function calls itself in order to solve a


problem. In recursive functions, a base case is typically used to stop the recursion and prevent
an infinite loop. Recursion can often simplify problems that have repetitive or nested
structure, such as calculations involving factorials, Fibonacci sequences, or tree traversals.

Example of Recursion: Factorial Calculation

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

C Code to Calculate Factorial Using Recursion

#include <stdio.h>

// Recursive function to calculate factorial


int factorial(int n) {
if (n == 0) {
return 1; // Base case: factorial(0) is 1
} else {
return n * factorial(n - 1); // Recursive case
}
}

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.

Q.4 Explain structure and its initialization

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

Let's say we want to define a structure to store information about a Book:

#include <stdio.h>

// Defining a structure named Book


struct Book {
char title[50];
char author[50];
int year;
float price;
};

int main() {
// Creating a variable of type struct Book
struct Book myBook;

// Accessing and assigning values to structure members


strcpy(myBook.title, "C Programming Language");
strcpy(myBook.author, "Brian W. Kernighan");
myBook.year = 1978;
myBook.price = 29.99;

// Printing the values stored in the structure


printf("Title: %s\n", myBook.title);
printf("Author: %s\n", myBook.author);
printf("Year: %d\n", myBook.year);
printf("Price: %.2f\n", myBook.price);

return 0;
}

Q.5 WAP to create employee database using structure

#include <stdio.h>

#include <string.h>

// Define a structure for Employee

struct Employee {

int id; // Employee ID

char name[50]; // Employee Name

float salary; // Employee Salary

};

// Function to input employee details

void inputEmployeeDetails(struct Employee *e) {

printf("Enter Employee ID: ");

scanf("%d", &e->id);

printf("Enter Employee Name: ");


getchar(); // To clear the newline character left by previous scanf

fgets(e->name, sizeof(e->name), stdin);

e->name[strcspn(e->name, "\n")] = '\0'; // Remove newline character from name

printf("Enter Employee Salary: ");

scanf("%f", &e->salary);

// Function to display employee details

void displayEmployeeDetails(struct Employee e) {

printf("\nEmployee Details:\n");

printf("ID: %d\n", e.id);

printf("Name: %s\n", e.name);

printf("Salary: %.2f\n", e.salary);

int main() {

// Declare a variable of type Employee

struct Employee emp;

// Input employee details

inputEmployeeDetails(&emp);

// Display employee details

displayEmployeeDetails(emp);

return 0;

You might also like