FPL unit 5
FPL unit 5
Reusability: One of the main benefits of user-defined functions is that they can be reused in
different parts of a program. Instead of writing the same code multiple times, you can define
a function and call it whenever you need to perform a specific task. It makes the code more
efficient and easier to maintain.
Simplified code: User-defined functions can simplify the code by abstracting away complex
logic into a single function. It makes the code easier to read and understand, reducing the
likelihood of errors and improving the overall quality of the program.
Better testing: By breaking down a program into smaller functions, it becomes easier to test
each function individually. It makes it easier to identify and isolate issues within the code,
making it easier to fix any bugs and improve the overall quality of the program.
3. Function call
Function Call
A function call is a statement that instructs the compiler to execute the function. We use the
function name and parameters in the function call.
In the below example, the first sum function is called and 10,30 are passed to the sum
function. After the function call sum of a and b is returned and control is also returned back
to the main function of the program.
Working of function in C
Some programmer thinks that defining a function inside an another function is known as
“nested function”.
Some programmer thinks that defining a function inside an another function is known as
“nested function”. #include<stdio.h>
int my_fun() {
printf("check_fun function");
printf("\n");
}
int main() {
my_fun();
printf("Main Function\n");
printf("Done");
}
Recursion
Recursion is the technique of making a function call itself. This technique provides a way to
break complicated problems down into simple problems which are easier to solve.
Recursion is the process which comes into existence when a function calls a copy of itself to
work on a smaller problem. Any function which calls itself is called recursive function, and
such function calls are called recursive calls. Recursion involves several numbers of recursive
calls. However, it is important to impose a termination condition of recursion. Recursion code
is shorter than iterative code however it is difficult to understand.
Recursion cannot be applied to all the problem, but it is more useful for the tasks that can be
defined in terms of similar subtasks. For Example, recursion may be applied to sorting,
searching, and traversal problems.
#include <stdio.h>
int fact (int);
int main()
{
int n,f;
printf("Enter the number whose factorial you want to calculate?");
scanf("%d",&n);
f = fact(n);
printf("factorial = %d",f);
}
int fact(int n)
{
if (n==0)
{
return 0;
}
else if ( n == 1)
{
return 1;
}
else
{
return n*fact(n-1);
}
}
Syntax:
void recurse()
{
... .. ...
recurse();
... .. ...
}
int main(){
... .. ...
recurse();
... .. ...
}
Ex:
struct book
{
char title[50];
char author[50];
double price; int pages;
};
C Structure Definition
To use structure in our program, we have to define its instance. We can do that by creating
variables of the structure type. We can define structure variables using two methods:
Example
The following statement demonstrates the initialization of structure
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
Example 1
The four elements of the struct variable book1 are accessed with the dot (.) operator.
Hence, "book1.title" refers to the title element, "book1.author" is the author name,
"book1.price" is the price, "book1.pages" is the fourth element (number of pages).
Take a look at the following example −
Open Compiler
#include <stdio.h>
struct book{
char title[10];
char author[20];
double price;
int pages;
};
int main(){
struct book book1 = {"Learn C", "Dennis Ritchie", 675.50, 325};
printf("Title: %s \n", book1.title);
printf("Author: %s \n", book1.author);
printf("Price: %lf\n", book1.price);
printf("Pages: %d \n", book1.pages);
printf("Size of book struct: %d", sizeof(struct book));
return 0;
}
Output
Run the code and check its output −
Title: Learn C
Author: Dennis Ritchie
Price: 675.500000
Pages: 325
Size of book struct: 48