FOC Unit 3: Function

 Functions in C Programming

A function in C is a block of code that performs a specific task.
It helps in code reusability, modularity, and makes programs easy to understand.

Why use functions?

To avoid repeating the same code.

To make code cleaner and organized.

To divide large programs into smaller parts.

Types of Functions

There are mainly two types:

1. Library Functions:

Already available in C libraries.

Examples:

printf()

scanf()

sqrt()

2. User-Defined Functions

Functions created by the programmer.

Function Syntax

return_type function_name(parameter_list) {
// code to be executed
}


Function Example (Simple Addition)

#include <stdio.h> int add(int a, int b) { // function definition return a + b; } int main() { int result = add(5, 10); // function call printf("Result = %d", result); return 0; }

Output:

Result = 15

Parts of a Function

  1. Function Declaration (Prototype)
Tells the compiler about the function name, return type, and parameters.
int add(int, int);

    2. Function Definition

Actual body of the function.


int add(int a, int b) {
return a + b; }
}

    3. Function Call

 

Used to use/execute the function.


add(5, 10);


Types Based on Input/Output

1. Without arguments and without return

void greet() {

printf("Hello!");

}

2. With arguments but no return

void square(int x) {

printf("%d", x * x);

}

3. Without arguments but with return

int getNumber() {

return 10;

}

4. With arguments and return (Most common)

int multiply(int a, int b) {

return a * b;

}

Example: Function to Find Factorial

#include <stdio.h> int factorial(int n) { int fact = 1; for (int i = 1; i <= n; i++) { fact *= i; } return fact; } int main() { int num = 5; printf("Factorial = %d", factorial(num)); return 0; }

Benefits of Functions

Reduces code duplication
Makes debugging easier
Allows team collaboration
Helps in modular programming


Call by Value in C

In call by value, when you pass arguments to a function, a copy of the actual values is passed.
This means:

  • The function works on copied values, not on the original variables.
  • Any changes made inside the function do NOT affect the original variables from the calling function.

How Call by Value Works

  • You call a function and pass variables.
  • C creates new memory locations for function parameters.
  • The function uses these values but cannot modify the originals.

Example

#include <stdio.h> void changeValue(int x) { x = 100; // modifying local copy } int main() { int a = 10; changeValue(a); printf("Value of a = %d", a); // output: 10 return 0; }


Explanation

a is 10 in main().

changeValue(a) sends a copy of a to the function.

Inside changeValue(), x becomes 100, but this does not affect a.

So output is still 10.


Recursion

Recursion is a programming technique where a function calls itself to solve a problem.

A recursive function solves a large problem by breaking it into smaller subproblems of the same type.

A recursive function must have:

1️⃣ Base Case (Stopping Condition)

Prevents infinite function calls

Tells the function when to stop

2️⃣ Recursive Case

The part where the function calls itself with a smaller problem

General Structure of a Recursive Function

returnType functionName(parameters) {

if (base condition) { return value; } else { return functionName(smaller problem); } }

Example 1: Factorial using Recursion

Factorial: 5! = 5 × 4 × 3 × 2 × 1

#include <stdio.h> int factorial(int n) { if (n == 0) // base case return 1; else return n * factorial(n - 1); // recursive call }


int main() { int num = 5; printf("Factorial = %d", factorial(num)); return 0; }

Passing an Array to a Function

when you pass an array to a function:

  • You actually pass the address of the first element (i.e., a pointer).
  • The function can access and modify the array elements.
  • Arrays are always passed by reference (indirectly).

Syntax: Passing an Array

You can pass an array in two common ways:

1️⃣ Using array syntax

void display(int arr[], int size);

2️⃣ Using pointer syntax

void display(int *arr, int size);


Example: Passing Array to Function

#include <stdio.h>

void display(int arr[], int size) { for(int i = 0; i < size; i++) { printf("%d ", arr[i]); } } int main() { int numbers[5] = {10, 20, 30, 40, 50}; display(numbers, 5); // passing array to function return 0; }

Post a Comment

0 Comments