Functions | #Tut-06

 So, today we are going to cover very important concept in C programming language which is nothing but the "functions" and "pointers".

Sometimes our program gets bigger in size and it's not possible for a programmer to track which piece of code is doing what.  Function is a way to break our code into chunks so that it is possible for a programmer to reuse them. 



Function's :

What is function ?

-->    A function is a block of code which performs a particular task. A function can be reused by the programmer in a given program for any number of times. 




Types of function..

C functions can be classified into two categories

1. library functions :  commonly required functions grouped together in a library file on                                                                     a disk.

2. user defined function :  these are the functions declared and defined by the user.


Why use functions..?

--> to avoid rewriting of same logic again and again

--> to keep track of what we are doing in program

--> to test and check logic independently

--> separating the code into modular functions also makes the program easier to design and understand


some advantages..

        - It saves time and energy as it can be reused

        - It helps in faster development

        - Debugging and testing is easier

        - Functions are helpful in generalizing the program


let us see one program..

/*
    Write a program with three funcitons
    1> good morning which prints "good morning.."
    2> good afternoon which prints "good afternoon"
    3> good night function prints "good night"

    main() should call all of these in order 1 --> 2 --> 3.
*/
#include <stdio.h>
void goodMorning()
{
    printf("good morning..\n");
}

void goodAfternoon()
{
    printf("good afternoon..\n");
}

void goodNight()
{
    printf("good night..");
}
int main()
{
    goodMorning();
    goodAfternoon();
    goodNight();
    return 0;
}

output :

good morning..
good afternoon..
good night..


Passing values to functions..

we can pass value to a function and can get  value in return from a function.

                                                            int sum(int a, int b){ }

The above prototype means that sum is a function which takes values a (int type) and b (int type)

and return a vlues of the type interger.

Now as I have declared the function sum, let us see how will be the definition of sum...


So far we have studied that what is the function..

Let us see one program related to library function...

#include<stdio.h>
#include <math.h>

// your task is to find the sin, cos, tan, log, sqrt and other math functoins..

int main(){

    // pow(), sqrt, sin() --> these functions are already defined in math.h header file
    // int num = 5;  5 ^ 3 = 125

    int num, power;
    printf("enter the number : ");
    scanf("%d", &num);

    printf("enter the power which you wanna find : ");
    scanf("%d", &power);

    //      5  ^  3  :  125
    printf("%d ^ %d is : %.2f", num, power, pow(num, power));
   

    // printf("\nsin(90) : %f", sin(90));
    return 0;
}

Output :

enter the number : 5
enter the power which you wanna find : 3
5 ^ 3 is : 125.00

Here #include<math.h> is a library function and the expression pow() is defined under this library function. It always takes a floating value or cannot be run without a "%f".


Recursion..

There is simple defination of recursion i.e. "a function call itself" . Any function which calls itself is called recursive function, and such function calls are called recursive calls. Recursion involves several numbers of recursive calls.  There are very good examples of recursion

e.g. factorial of number, fibonacci series, etc.




So at the end our recursion should stop at the one stage i.e we need to specify the base condition and as we know factorial(1)  or factorial(0) is always  1. 


Code Snippet :

#include<stdio.h>

int fact(int num)
{
    if (num == 0 | num == 1)
    {
        return 1;
    }
    return num * fact(num-1);
}

int main()
{
    printf("enter the number you wanna factorail of : ");
    int num;
    scanf("%d", &num);

    int factorial = fact(num);

    printf("the factorial of %d is %d ", num, factorial);
    return 0;
}


Output : 

enter the number you wanna factorail of : 5
the factorial of 5 is 120



Pointers..

A pointer is a variable which stores the address of another variable.


the value at (*) and address of (&) operator..

e.g  int i = 3;

            int *j = &i;

when i want to access the address of i then i will use -->  j

and when i want to access value of j then i will use --> *j

Code Snippet : 

#include<stdio.h>
// pointer is a variable which stores the address of another variable..
// & --> is used to acces the address of variable..
// * --> is used to value present at that address

int main(){

    // dealing with pointers..😎
    // * --> denotes the pointer
    int i = 133;
    int *ptr = &i;
    // int **p = &ptr; // this stores the address of ptr of type pointer to pointer..
    // int ***q = &p;

    printf("value of i : %d\n", i);
    printf("address of i : %d\n", ptr); // i = 2343423
    printf("value of ptr : %d\n", *ptr); // i = 133
    printf("address of ptr : %d\n", &ptr); // i = 133
    // printf("address of ptr : %d", p);
    return 0;
}

Output :

value of i : 133
address of i : 6422300
value of ptr : 133
address of ptr : 6422296


Types of function call..


1. Call by value..

This won't swap the values as we are passing the values only and not the address, so only the copy of that variables will be passed to function.


2. Call by reference..


This will definitely swap the values as we are passing the address of the variables so, the changes will affect the original values also and this is nothing but our call by reference.



Comments

Popular posts from this blog

Introduction to C language | #Tut-01

Loop statements | do-while | while | for | #Tut-05