Follow

Functions & Pointers || chapter 5

  1. What is a Function:

  1. A function is a block of code that performs a specific task. It is a self-contained unit of code that can be called from other parts of a program. Functions in C have a return type, a name, and parameters.

Example:

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

This function takes two integer parameters and returns their sum.

  1. Why Use Functions:

    Functions provide a modular and organized way to write code. They allow code to be reused and make it easier to maintain and debug. Functions also improve readability and reduce the amount of code duplication.

  2. Passing Values between Functions:

    Values can be passed between functions through parameters. When a function is called, the values of its arguments are copied into the corresponding parameters of the function.

Example:

c
int main() {
    int a = 5, b = 10
    int sum = add(a, b); 
     printf("The sum of %d and %d is %d", a, b, sum); 
    return 0
int add(int a, int b)
    return a + b; 
}

Here, the main function calls the add function and passes the values of a and b as arguments.

  1. Scope Rule of Functions:

  1. The scope of a variable inside a function is limited to that function. Variables declared inside a function cannot be accessed outside that function.

Example:

c
int main()
    int x = 5
     func();
    printf("%d", x); 
    return 0;
 } 
void func()
    int y = 10
    printf("%d", y); 
}

Here, x is declared inside the main function and y is declared inside the func function. x cannot be accessed inside the func function, and y cannot be accessed outside the func function.

  1. Calling Convention:

    The calling convention is the set of rules for how function calls are made and parameters are passed between functions. In C, the default calling convention is the cdecl convention, which passes parameters on the stack from right to left.

  2. One Dicey Issue:

    One dicey issue in C is the use of uninitialized variables. If a variable is declared but not initialized, it can contain a random value that can lead to unexpected behavior.

Example:

c
int main() {
    int x; 
    printf("%d", x); 
    return 0
}

Here, x is declared but not initialized. When printed, it can contain any random value.

  1. Advanced Features of Functions:

    Advanced features of functions include variable-length argument lists, function pointers, and recursion.

  2. Function Declaration and Prototypes:

    Function declaration is the process of telling the compiler about the existence of a function before it is called. Function prototypes are declarations that specify the return type, name, and parameters of a function.

Example:

c
int add(int, int)
int main()
    int a = 5, b = 10
    int sum = add(a, b);
    printf("The sum of %d and %d is %d", a, b, sum); 
    return 0
}
int add(int a, int b)
return a + b;
 }

Here, the add function is declared before it is called in the main function.

  1. Call by Value and Call by Reference:

  1. Call by Value and Call by Reference are two ways in which function arguments are passed in C programming language.

Call by Value:

In Call by Value, a copy of the argument value is passed to the function. The function cannot modify the original value.

Example:

c
void swap(int a, int b)
    int temp;
     temp = a;
     a = b; 
     b = temp; 
}
int main()
    int x = 5, y = 10
    swap(x, y); 
    printf("x = %d, y = %d", x, y); 
    return 0
}

Output: x = 5, y = 10

Call by Reference:

In Call by Reference, the address of the argument is passed to the function. The function can modify the original value using the address.

Example:

c
void swap(int *a, int *b)
    int temp; 
    temp = *a; 
    *a = *b; 
    *b = temp; 
}
int main()
    int x = 5, y = 10
    swap(&x, &y); 
    printf("x = %d, y = %d", x, y); 
    return 0
}

Output: x = 10, y = 5

  1. An Introduction to Pointers:

  1. A pointer is a variable that stores the memory address of another variable.

Example:

c
int main() { 
    int x = 5
    int *ptr; 
    ptr = &x
    printf("The value of x is %d\n", x); 
    printf("The value of ptr is %p\n", ptr); 
    printf("The value pointed to by ptr is %d\n", *ptr); 
    return 0;
 }

Output: The value of x is 5 The value of ptr is 0x7fff56f8cfdc The value pointed to by ptr is 5

  1. Pointer Notation:

  1. Pointer Notation is a way to access variables through pointers.

Example:

c
int main() { 
    int arr[5] = {1, 2, 3, 4, 5}; 
    int *ptr; ptr = arr; 
    printf("The first element of arr is %d\n", *ptr); 
    printf("The third element of arr is %d\n", *(ptr+2));
    return 0;
 }

Output: The first element of arr is 1 The third element of arr is 3

  1. Back to Function Calls:

  1. Pointers can also be used as function arguments.

Example:

c
void swap(int *a, int *b)
    int temp; 
    temp = *a; 
    *a = *b;
    *b = temp; 
int main()
    int x = 5, y = 10
    swap(&x, &y); 
    printf("x = %d, y = %d", x, y); 
    return 0
}

Output: x = 10, y = 5

  1. Recursion:

  1. Recursion is a technique where a function calls itself until a base case is reached.

Example:

c
int factorial(int n) {
    if (n == 0) { 
        return 1
     } else
        return n * factorial(n-1);
     }
 } 
int main()
    int n = 5
    printf("%d! = %d", n, factorial(n)); 
    return 0;
 }

Output: 5!

  1. Recursion and Stack:

When a function calls itself recursively, each call creates a new stack frame on the call stack. The call stack is a data structure that keeps track of the order in which functions are called and their parameters and local variables.

Example:

c
int sum(int n) {
    if (n == 0) { 
        return 0
     } 
    else
        return n + sum(n-1); 
     } 
}

In this example, the sum function calls itself with a smaller input, which creates a new stack frame. Each stack frame stores the value of n and the return address. When the base case is reached, the stack is popped, and the function returns the sum of all the values.

No comments:

Post a Comment

Tell us how you like it.