1 of 33

Function in C

2 of 33

Functions

  • A Function in C is a block of statements that is defined to perform a specific task.

  • A function is also called a reusable code because once it is created, it can be used again and again.

3 of 33

Function types�

  • In C, there are two types of functions available, and they are:

  • Built-in Functions : These functions are predefined, and we can use them any time we want in our C program. For example printf(), scanf(), gets(), etc.

  • User Defined Functions : These functions are defined or created by the programmer for performing a specific task in a program.

4 of 33

Function Syntax�

  • return_type function_name(parameter_list)

{

body of the function

}

5 of 33

  • Explanation of each part:
  • Return type: Specifies the type of value the function will return. Use void if the function does not return anything.
  • Function name: A unique name that identifies the function. It follows the same naming rules as variables.
  • Parameter listA set of input values passed to the function. If the function takes no inputs, this can be left empty or written as void.
  • Function body: The block of code that runs when the function is called. It is enclosed in curly braces { }.

6 of 33

Function prototype

7 of 33

Categorization based on arguments and return value

  • Function with no arguments and no return value
  • Function with no arguments and return value
  • Function with arguments but no return value
  • Function with arguments and return value.

8 of 33

Advantages of Functions in C�

  • Modularity: Functions break down large, complex programs into smaller, self-contained, and manageable blocks of code, each designed to perform a specific task..
  • Code Reusability: Once a function is defined, it can be called multiple times throughout the program or even in other programs, eliminating the need to rewrite the same code repeatedly. This saves development time and effort.
  • Readability and Understandability: By using descriptive function names and separating concerns, the main logic of the program becomes cleaner and easier to read.

9 of 33

  • Easier Debugging and Testing: The modular nature of functions allows individual blocks of code to be tested and debugged in isolation
  • Abstraction: Functions provide a layer of abstraction, allowing programmers to focus on the high-level logic of the program without worrying about the low-level implementation details of every task (e.g., using printf()
  • Easier Maintenance: With code organized into functions, modifications or updates to a specific functionality only require changes within that function's definition, simplifying program maintenance. 

10 of 33

  • Three Main Parts of a Function

  • Function Declaration (Function prototype)
  • • Function Definition
  • • Function Call

11 of 33

Categorization based on arguments and return value

  • Function with no arguments and no return value
  • • Function with no arguments and return value
  • • Function with arguments but no return value
  • • Function with arguments and return value.

12 of 33

  • A function: receives zero or more parameters, performs a specific task, and returns zero or one value
  • A function is invoked / called by name and parameters
  • Communication between function and invoker code is through the parameters and the return value
  • In C, no two functions can have the same name

13 of 33

call by Value�

  • In pass by value, the function receives a copy of the variable's value.
  • Any changes made to the parameter inside the function do not affect the original variable in the caller.
  • Two separate copies exist in memory: The original variable in the caller and the function's local copy

14 of 33

call by Reference�

  • In pass by reference, the function receives the address of the variable instead of a copy.

  • The function can directly modify the original variable in the caller.
  • Only one memory location exists for the variable, so changes inside the function affect the original variable.
  • In C, this is done using pointers, as C does not have pass by reference like C++. Instead, you can pass the address of a variable to a function using pointers.

15 of 33

Recursive function

16 of 33

Recursive function

  • A recursive function in C is a function that calls itself, either directly or indirectly, to solve a problem by breaking it down into smaller, simpler sub-problems.
  • This technique requires two main components to work correctly:
  • a base case (stopping condition) and a recursive case (the part where the function calls itself).

17 of 33

Base Case:

  • Base Case: This is the essential termination condition that stops the recursion and prevents infinite loops or a stack overflow error. �

18 of 33

Recursive Case:

  • Recursive Case: This part of the function contains the logic where the problem is broken down into a smaller version of itself, and the function calls itself with modified parameters, moving closer to the base case with each call.

19 of 33

Call Stack:

  • Call Stack: Each time a function is called, a new "stack frame" is added to the call stack in memory. This frame stores the function's local variables, parameters, and the return address.

20 of 33

Example 2: Fibonacci using Recursion

  • int fib(int n)
  • {
  • if (n == 0) // Base case 1
  • return 0;
  • else if (n == 1) // Base case 2
  • return 1;
  • else
  • return fib(n-1) + fib(n-2); // Recursive case
  • }

21 of 33

Example 3: Sum of first n numbers�

  • int sum(int n)
  • {
  • if (n == 0) // Base case
  • return 0;
  • else
  • return n + sum(n - 1); // Recursive case
  • }

22 of 33

Storage classes

  • Storage classes in C determine the scope, lifetime, default value, and memory location of variables and functions. They provide instructions to the compiler on how to manage the variable's memory and visibility.

23 of 33

Storage classes

  • C has four primary storage classes, defined by the keywords
  • auto
  • register
  • static
  • extern.

24 of 33

The auto Storage Class

  • This is the default storage class for all the variables declared inside a function or a block.
  • Auto variables can be only accessed within the block/function they have been declared and not outside them (which defines their scope).

25 of 33

Example of auto Storage Class�

  • The following code statements demonstrate the declaration of an automatic (auto) variable −

  • {
  • int i;
  • auto int j;
  • }

26 of 33

Properties of auto Variables

  • Scope: Local
  • Default Value: Garbage Value
  • Memory Location: stack (RAM)
  • Lifetime: Till the end of its scope

27 of 33

The register Storage Class

  • The register storage class is used to define local variables that should be stored in a register instead of RAM.
  • This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location).

28 of 33

The register Storage Class

  • Scope: Local
  • Default Value: Garbage Value
  • Memory Location: Register in CPU or RAM
  • Lifetime: Till the end of its scope

29 of 33

The static Storage Class�

  • The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

30 of 33

The static Storage Class

  • Scope: Local
  • Default Value: Zero
  • Memory Location: RAM
  • Lifetime: Till the end of the program

31 of 33

�The extern Storage Class��

  • The extern storage class is used to give a reference of a global variable that is visible to ALL the program files.
  • When we use 'extern', the variable cannot be initialized however, it points the variable name at a storage location that has been previously defined.

32 of 33

The extern Storage Class

  • Properties of extern Storage Class Objects

  • Scope: Global
  • Default Value: Zero
  • Memory Location: RAM
  • Lifetime: Till the end of the program.

33 of 33