BCA Sem1 C Unit-III
BCA Sem1 C Unit-III
Unit III
Functions in C
Types of functions in C
There are two main types of functions in programming:
A. Library Function
1. In C programming, a library function (or built-in function) refers to a function that is
provided as part of the standard C library.
2. These functions are predefined and come with the C programming language.
3. Library functions are designed to perform common and useful operations, making them
readily available for C programmers to use.
4. Library functions are already defined in C, so you don't need to declare or define them
yourself.
5. Many library functions are part of the Standard C Library, such as functions for
input/output (e.g., printf, scanf), string manipulation (e.g., strlen, strcpy), mathematical
operations (e.g., sqrt, sin), and more.
6. Library functions are well-documented in C language specifications and library
documentation. You can easily find information on how to use these functions and their
required parameters.
7. E.g.
NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 1
www.nandigramiit.org
C PROGRAMMING UNIT III
#include <stdio.h>
#include <math.h> // Include the math library for sqrt() function
int main()
{
double number = 16.0;
double result = sqrt(number); // Using the sqrt() library function
printf("The square root of %f is %f\n", number, result);
return 0;
}
8. In the code above, we included the math library (using #include <math.h>) to access
the sqrt() function, which calculates the square root of a number.
Type: Functions that do not return any value are called "void functions."
3.3.1 Declaration: You declare a void function by specifying its name, parameters, and the
void keyword as the return type.
For example:
3.3.2 Definition: The definition of the function contains the actual code to be executed when
the function is called.
For example:
void greetUser(char name[]) {
printf("Hello, %s!\n", name);
}
3.3.3 Function Calling: You call a void function in your program by using its name and
providing any required arguments.
For example:
int main() {
char username[] = "John";
greetUser(username); // Call the greetUser function
return 0;
}
3.3.1 Declaration: You declare a function with a return value by specifying its name,
parameters, and the data type of the return value.
For example:
3.3.2 Definition: The definition of the function contains the code to be executed and a return
statement to send back a value of the specified data type.
For example:
3.3.3 Function Calling: You call a function with a return value in your program and store the
returned value in a variable.
For example:
int main() {
int result = add(3, 4); // Call the add function and store the result
printf("The sum is %d\n", result);
return 0;
}
3.4 Recursion
1. Recursion is a programming technique where a function calls itself in order to solve a
problem.
2. In C, a recursive function is a function that, within its own definition, makes one or
more calls to itself.
3. This can be a powerful way to solve problems that can be broken down into smaller,
similar sub-problems.
4. Here's the basic structure of a recursive function in C:
return_type function_name(parameters) {
// Base case (exit condition)
if (base_case_condition) {
// Return a value
}
// Recursive case
else {
// Make one or more recursive calls
// Modify parameters
}
}
#include <stdio.h>
int main() {
int num = 5;
int result = factorial(num);
printf("Factorial of %d is %d\n", num, result); // Output: Factorial of 5 is 120
return 0;
}
In this example, the factorial function is defined recursively. The base case checks if n is 0 and
returns 1 because the factorial of 0 is defined as 1. In the recursive case, it calculates the
factorial of n by multiplying n with the factorial of (n - 1). This process continues until the base
case is reached.
At this point, the base case is reached, and each function call starts returning values:
factorial(0) returns 1
factorial(1) returns 1 * 1 = 1
factorial(2) returns 2 * 1 = 2
factorial(3) returns 3 * 2 = 6
factorial(4) returns 4 * 6 = 24
factorial(5) returns 5 * 24 = 120