ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
Functions and Procedures in C
1. Introduction to Functions
In C programming, a function is a block of statements grouped together to perform a specific
task. Functions help in breaking a large program into smaller, more manageable parts. The use of
functions enhances code reusability, readability, and modularity.
Key Benefits of 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 C function is made up of three main components:
2.1 Function Declaration (Prototype):
- 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
MR. ABANDA JOSHUA
return_type function_name(parameter_list);
Example:
```c
int add(int a, int b); // Function declaration
```
2.2 Function Definition:
- 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) {
// Function body (logic)
return value; // For non-void functions
```
Example:
```c
int add(int a, int b) {
return a + b; // Returns the sum of a and b
```
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
---
2.3 Function Call:
- 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
```
3. Example of a Complete Function Program in C
Let’s look at a full program that demonstrates how functions work in C:
```c
include <stdio.h>
// Function declaration
int add(int a, int b);
void printResult(int result);
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
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
}
// Function definition to print the result
void printResult(int result) {
printf("The sum is: %d\n", result);
}
```
---
4. Types of Functions in C
4.1 Built-in Functions:
These functions are part of the C standard library, and they come with the language. Examples of
built-in functions include:
- `printf()`: Prints output to the screen.
- `scanf()`: Reads input from the user.
- `strlen()`: Returns the length of a string.
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
- `sqrt()`: Returns the square root of a number.
4.2 User-defined Functions:
These are functions created by the user to perform specific tasks. The user writes both the
function declaration and definition.
Example 1: User-Defined Function for Subtraction
```c
include <stdio.h>
int subtract(int x, int y); // Function declaration
int main() {
int result = subtract(10, 5); // Function call
printf("The difference is: %d\n", result);
return 0;
}
// Function definition for subtraction
int subtract(int x, int y) {
return x - y; // Returns the difference of x and y
}
```
Example 2: Function to Find the Maximum of Two Numbers
```c
include <stdio.h>
int findMax(int a, int b); // Function declaration
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
MR. ABANDA JOSHUA
// Function definition to find the maximum of two numbers
int findMax(int a, int b) {
if (a > b)
return a;
else
return b;
}
```
---
5. Function Parameters
5.1 Formal Parameters:
These are the variables defined in the function declaration/definition that receive the values
passed from the calling function.
5.2 Actual Parameters:
These are the real values or variables passed to the function during the function call.
Example:
```c
int add(int a, int b); // a and b are formal parameters
add(5, 3); // 5 and 3 are actual parameters
```
6. Function Return Types
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
6.1 Returning Values:
- A function can return a value using the `return` keyword.
- The return type must match the type specified in the function declaration.
Example 1: Function Returning an Integer
```c
include <stdio.h>
int square(int num); // Function declaration
int main() {
int result = square(4); // Function call
printf("The square is: %d\n", result);
return 0;
}
// Function definition to calculate square
int square(int num) {
return num * num; // Returns the square of num
}
Example 2: Function Returning a Float
```c
include <stdio.h>
float divide(float a, float b); // Function declaration
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
int main() {
float result = divide(10.0, 3.0); // Function call
printf("The quotient is: %.2f\n", result);
return 0;
}
// Function definition to divide two numbers
float divide(float a, float b) {
return a / b; // Returns the quotient
}
```
7. Void Functions (Procedures) in C
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.
Example 1: Void Function to Print a Message
```c
include <stdio.h>
void printMessage(); // Void function declaration
int main() {
printMessage(); // Function call
return 0;
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
// Function definition to print a message
void printMessage() {
printf("Hello, World!\n");
}
```
Example 2: Void Function to Print an Array
```c
include <stdio.h>
void printArray(int arr[], int size); // Void function declaration
int main() {
int arr[] = {1, 2, 3, 4, 5};
printArray(arr, 5); // Function call
return 0;
}
// Function definition to print the array elements
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
```
ADVANCED DATASTRUCTURES/ADVANCED ALGORITHMS (HISMIL) CLASS II
MR. ABANDA JOSHUA
8. Differences Between Functions and Procedures in C
| Functions | Procedures (Void Functions) |
|---------------------------------------------------------|----------------------------------------------------------
----|
| Returns a value after execution. | Does not return any value.
|
| 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
MR. ABANDA JOSHUA
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.