Advanced Data Structures - Advanced Algorithms Class II
Advanced Data Structures - Advanced Algorithms Class II
1. Introduction to Functions
1. Reusability: Once a function is written, it can be reused multiple times without rewriting the
code.
2. Modularity: Functions break the code into modules or blocks, making it easier to maintain and
debug.
3. Abstraction: The function user does not need to know the internal details of how the function
works. They only need to know how to call it.
2. Components of a Function in C
- A function declaration is an announcement to the compiler about a function's name, return type,
and the parameters it accepts.
- The declaration must appear before the function is used (typically at the beginning of the
program or in a header file).
Syntax:
```c
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
return_type function_name(parameter_list);
Example:
```c
```
- This is where the function's actual logic is written. The definition includes the function’s body
that contains the code to be executed when the function is called.
Syntax:
```c
return_type function_name(parameter_list) {
```
Example:
```c
```
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
---
- A function call is used to invoke a function so that the function's code can execute. The call
transfers control to the function.
Syntax:
```c
function_name(arguments);
```
Example:
```c
int result = add(5, 7); // Calls the add() function with arguments 5 and 7
```
```c
include <stdio.h>
// Function declaration
int add(int a, int b);
void printResult(int result);
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
int main() {
int num1 = 5, num2 = 7;
int sum = add(num1, num2); // Function call
printResult(sum); // Function call to display result
return 0;
}
// Function definition for addition
int add(int a, int b) {
return a + b; // Return the sum of a and b
}
---
4. Types of Functions in C
These functions are part of the C standard library, and they come with the language. Examples of
built-in functions include:
These are functions created by the user to perform specific tasks. The user writes both the
function declaration and definition.
```c
include <stdio.h>
int main() {
int result = subtract(10, 5); // Function call
printf("The difference is: %d\n", result);
return 0;
}
```c
include <stdio.h>
int main() {
int num1 = 10, num2 = 20;
int max = findMax(num1, num2); // Function call
printf("The maximum is: %d\n", max);
return 0;
}
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
---
5. Function Parameters
These are the variables defined in the function declaration/definition that receive the values
passed from the calling function.
These are the real values or variables passed to the function during the function call.
Example:
```c
```
- The return type must match the type specified in the function declaration.
```c
include <stdio.h>
int main() {
int result = square(4); // Function call
printf("The square is: %d\n", result);
return 0;
}
```c
include <stdio.h>
int main() {
float result = divide(10.0, 3.0); // Function call
printf("The quotient is: %.2f\n", result);
return 0;
}
In C, a procedure is a function that does not return any value, which is defined using the `void`
return type. Procedures are typically used to perform operations that do not require a result to be
returned.
```c
include <stdio.h>
int main() {
printMessage(); // Function call
return 0;
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
```c
include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5); // Function call
return 0;
}
|---------------------------------------------------------|----------------------------------------------------------
----|
| Requires a return type such as `int`, `float`, `char`, etc. | Uses the `void` return type.
|
| Often used for calculations or returning data. | Often used for performing tasks such as
printing or modifying variables. |
| Example: `int sum(int a, int b)` returns an integer. | Example: `void printMessage()` prints a
message without returning anything. |
9. Exercises
Exercise 1:
- Write a function `multiply(int a, int b)` that multiplies two numbers and returns the result. Call
the function in `main()` and display the result.
Exercise 2:
- Write a `void` function `displayGreeting()` that prints a greeting message. Call the function in
`main()`.
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
Exercise 3:
- Write a function `isPrime(int n)` that returns `1` if a number is prime and `0` if it’s not. Use this
function in `main()` to check whether a number entered by the user is prime.
Exercise 4:
- Write a function `findMin(int a, int b)` that returns the minimum of two numbers. Use this
function in `main()` to find the minimum of two user-entered numbers.