0% found this document useful (0 votes)
7 views6 pages

BCA Sem1 C Unit-III

Unit III of the C Programming course covers functions, detailing their definition, types, and usage. It explains library functions, user-defined functions (both void and with return values), and introduces recursion as a programming technique. Examples are provided to illustrate the concepts of function declaration, definition, calling, and recursion in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

BCA Sem1 C Unit-III

Unit III of the C Programming course covers functions, detailing their definition, types, and usage. It explains library functions, user-defined functions (both void and with return values), and introduces recursion as a programming technique. Examples are provided to illustrate the concepts of function declaration, definition, calling, and recursion in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

C PROGRAMMING UNIT III

Unit III
Functions in C

3.2 What is Function?


1. In computer programming, a function is a self-contained block of code that performs a
specific task or set of tasks.
2. Functions are used to organize and modularize code, making it more readable,
maintainable, and reusable.
3. A function takes input (parameters or arguments), processes that input, and typically
returns an output (result).
4. Functions help break down a program into smaller, manageable pieces, which can be
individually developed, tested, and maintained.
5. Each function has a name, a set of parameters (if any), a function body, and a return
type.

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.

3.3 User defined functions

1. In C programming, user-defined functions are functions that you create to perform


specific tasks within your program.
2. These functions are defined by the programmer and are not part of the standard C
library.
3. User-defined functions allow you to break down your program into smaller, more
manageable parts, making your code more organized, readable, and modular.
4. Here are the types of user-defined functions in C:
A. Function without Return Value (Void Function):

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:

void greetUser(char name[]);

3.3.2 Definition: The definition of the function contains the actual code to be executed when
the function is called.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 2


www.nandigramiit.org
C PROGRAMMING UNIT III

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;
}

B. Function with Return Value:


Type: Functions that return a value of a specific data type are called "functions with return
values."

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:

int add(int a, int b);

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:

int add(int a, int b) {


int sum = a + b;
return sum;
}

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 3


www.nandigramiit.org
C PROGRAMMING UNIT III

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
}
}

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 4


www.nandigramiit.org
C PROGRAMMING UNIT III

5. In the recursive function:


i. The base case is the condition that determines when the recursion should stop. Ii. It's
essential to prevent infinite recursion and provide a result.
iii. The recursive case is where the function makes one or more calls to itself, typically
with modified parameters.

Here's an example of a simple recursive function in C to calculate the factorial of a number:

#include <stdio.h>

// Recursive function to calculate the factorial of a number


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

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.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 5


www.nandigramiit.org
C PROGRAMMING UNIT III

When calling factorial(5), it will result in the following recursive calls:

factorial(5) calls factorial(4)


factorial(4) calls factorial(3)
factorial(3) calls factorial(2)
factorial(2) calls factorial(1)
factorial(1) calls factorial(0)

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

Recursion is a powerful technique, but it should be used with care.


You must ensure that your recursive function reaches the base case to avoid infinite recursion.
It is often used for problems that can be naturally divided into smaller instances of the same problem,
like factorials, Fibonacci numbers, etc.

NANDIGRAM INSTITUTE OF INFORMATION TECHNOLOGY,NANDED 6


www.nandigramiit.org

You might also like