Header Ads

Function:C Programming Language - Part 5

Function



Function is the most fun thing of programming. Previously we used printf, scanf, toupper, tolower, etc. They are provided with programming language so that we can easily write programs. In this chapter we will learn how to write functions like your own needs.

Function is a reusable code block. Which could do a specific job. Knowing functions well, learning a great part of the programming is over. It can be written code itself.


Function Writing Rules
A function is written as below

1
2
3
4
Return_type function_Names {parameters} {
    Function body;
    Return value;
}
Return_Type is the type of data that the function returns. Such as int, char, float, double etc.
Function should have a name, is not it? By calling that function we will use the function. Function_Name is the name of the function.
Parameters is what we will give data to the function. Here we can give one or several parameters. There may be no Parameter in some functions. It depends on what type of function is being written. If there is more than one parameter, they have to write in the comma.
We type some code in Function Body. That's the main part of the function. The function does the main job here.
After the end of the work, the function returns and then passes with return.

We think of a small function. For example, the function of a number of square or square extract. In that function we will give a number as the parameter. The function will return the class of our number to that number. We can write the function as follows:

1
2
3
4
Int square (int num) {
    Int result = num * num;
    Return result;
}
All the functions of a function are in the above program. For example, when we first see the function int, we can say it returns an int data return. Next is the name of the function. After the function's parameters Where we call the function, we will give an integer number.


In the body of the function we have declared an integer variable, whose name is the result. And in which the integer variables found in the function's parameters are multiplied by itself. Because we know, by multiplying a number by that number, the number square or square is found. And finally we returned the result variable.


Calling Function

To use a function, call him. To call, we just write the name of that function. Thus:

Function_name (argument)


The function that we need to take to work, we have to pass it as a argument. As we have written the function above square, where the integer number must be passed as argument. Thus:

Square (5);


Here we can pass any integer number. The function will not work if you pass any other data. A complete program below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <stdio.h>
Int square (int num) {
    Int result = num * num;
    Return result;
}
Int main (void) {
  Int x = square (5);
  Printf ("Square is:% d", x);
  Return 0;
}
At first we wrote the function. Then I wrote the main function. Main itself is a function. Each program has a function named main. The compiler program first finds this main function while running. Going to the main function, we see that a variable named int x is taken. And its value is a function called square (5). Then find the function called squire.


When we call the square function from the main function, we gave a number. If I did not give this number, the program would not run. That means we have to give an integer value function. Here we can only give integer numbers. Because we told the function while writing the function, the function will take an integer number as the parameter. If we give some other arguments, our function can start madness!
Now if we pass any number when calling the square function, it will return the square of our number. And we put the return in a variable. Then we print it.


Now look at the function, can we just shorten the function? Yes i can We can not declare a variable within the function and write the function as follows:

1
2
3
Int square (int num) {
    Return num * num;
}
Again we can also call the function from the printf function. Full program below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
Int square (int num) {
    Return num * num;
}
Int main (void) {
  Printf ("Square is:% d", square (5));
  Return 0;
}
From this we know, we can call another function from within a function. One from the other, one from the other, so much as much as we can [understand ourselves the code].


You can peep a question on your head. What is the parameter key, and the arguments What is the difference between these two The data we pass while calling the function is the argument. For example 5 is the argument of square (5) arguments. And when we write functions, we declare the variable that is the parameter. For example, num is the parameters in square (int num).


Functions without parameters:

There may be some functions that may not have any parameters. So do not have to pass an argument when calling them. Such a function called pi. Whichever call it will return the value of our pi 3.1416. But do not have to pass an argument when calling him.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
Float pi () {
    Return 3.1416;
}
Int main (void) {
    Printf ("% f", pi ());
    Return 0;
}
Return functions without return type

Some functions may be something that can return nothing. When we write them we write void in the return type space. Such a function called happy. There is no sadness in the mind. Whenever he is called, he says: I am happy!

1
2
3
4
5
6
7
8
9
10
11
#include <stdio.h>
Void happy () {
    Printf ("I am happy!");
}
Int main (void) {
    Happy ();
    Return 0;
}
The functions that I have written so far were very easy. We can write a lot of programs using functions. For example, we may have to find a number of factories in real life. We know what is factorial, is not it? Factorial is the number of all integers, which is less than the number of factorial being zero. N is integer, then the factorial of n is expressed as: n! Such as 5! Its value will be 120



Now we write a program to find out the factorial Factorial

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
Long int factorial (int n) {
    Int i;
    Long int result = 1;
    If (n> 1) {
        For (i = 2; i <= n; i ++)
        Result = result * i;
    }
    Return result;
}
Int main ()
{
    Int num;
    Scanf ("% d", & num);
    Printf ("Factorial of% d is:% d", num, factorial (num));
    Return 0;
}
Here we write factorial a function which can find factorial. If we give this function as any number of arguments, then we will return a factual return to that number.


Functions with more than one parameter


We can use as many parameters as we want in a function. See the following program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
Int max (int x, int y) {
    Return (x> y)? X: y;
}
Int main ()
{
    Printf ("max is% d", max (8,12));
    Return 0;
}
Here we have written a function, which has two integer variables as its parameters. Then, after calling the function from main function, we gave two numbers. The function returned the number of those two numbers.




Function Prototype

We have written the functions that we have been writing before main. We can write after the main if we wish. But for him, the main function must be declared before it. Which means Function Prototype Function prototype is written as below
Return_type function_name (parameters);


The function that we have written in the square, at the beginning of this chapter, if we write after the main function, then we would write the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
Int square (int num);
Int main (void) {
  Printf ("Square is:% d", square (5));
  Return 0;
}
Int square (int num) {
    Return num * num;
}
We've got to understand what the function prototype is, how to write it. Let's write a few more complex programs.


Any number greater than 1 is prime or prime number, when it is not divisible by any number other than 1 and that number. Such as 2, 3, 5, 7, 11, 13, 17, 19 etc. We will write a function, which we will give a number. And the function will say, this is not a prime or prime. Full Program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
Void check_prime (int num);
Int main ()
{
    Int a;
    Scanf ("% d", & a);
    Check_prime (a);
    Return 0;
}
Void check_prime (int num) {
    Int i, count = 0;
    For (i = 2; i <= num / 2; i ++) {
        If (num% i == 0) {
            Count ++;
            Break;
        }
    }
    If (count == 0 && num! = 1)
    Printf ("% d is a prime number", num);
    Else
    Printf ("% d is not a prime number", num);
}
Here we are writing a program to check the prime number.


At the beginning of the program, we have stated that check_prime is our program which will not return anything.

We took a number from the user. We passed that number as an argument at check_prime. Our function then says that the number is not prime prime minister.


Recurrence or recursive function


If something is called by itself, then it is recurrence / recursion.

If you call yourself in an empty field or call in your name in a huge room, you can feel the recession.


Search by recursion in Google search. Will be written repeatedly Did you mean: Recursion. After the spell is correct, it will show. Google has made a rickshaw with a fun recursion search term.

Recursive Algorithm is the algorithm that calls itself, it does. Recruitment is used in a program using a recursive algorithm in computer programming. Recording is applied by calling itself a function in different programming languages. In C programming, how do the recursion or recursive functions be written?


What is the recursive function now it is easy to say? The function that calls itself, so is the recursive function.

With the ease of recursion, the code is easy to solve, instead of writing a lot of code, it can solve a problem with a few lines of code. Let us see the following sudocod

1
2
3
Void f () {
    F () ...
}
This is a recursive function, because the function calls itself. Call it directly to the call. Again the function can call a function without calling directly, the function that calls the first function. See the following generosity

1
2
3
4
5
6
Void f () {
    G () ...
}
Void g () {
    F () ...
}
The f function calls the g function. Again g function called the function f. This is also a recursive function.


We write a recursive function, which will take a number from the user, then from that number will print all numbers from 1 to that number. This is a simple program. But we will write this program a new way. Using recursion. Since the program is simple, then we can understand it a little better. And if we understand the way the program works, we will understand the recurrence. Then we can easily write complexes using all program recursions. Write the function before the Pseudo code [Sudo code], then see the implications of C programming.
Sudo means false. The Sudo Pseudo code is a program or an algorithm that can be used by ordinary people to write some code. These are not written using any programming language. It is written in such a way that people can understand. Below is a code named printInt, which prints all numbers from 1 to the number.

1
2
3
4
5
6
7
PrintInt (k) {
    If (k == 0) {
        Return 0;
    }
    Print (k); // calling itself
    PrintInt (k - 1);
}
The function will take an integer as the parameter. Then check the number 0? If there is zero, then the function of the function is finished. If 0 is not, then print the integer. And by subtracting those integers from 1, they will call printInt again. It means that you will call yourself.


Now if we pass 2 in the printInt function, then three copies of the function will be created. One is the value of k for 2, one is the value of k for 1. And one is the value of k for 0.


When the value of k is 2
PrintInt (int k) {
If (k == 0) {
Return 0;
}
Print (k);
PrintInt (k - 1);
} Will print 2 when the value of k is 1
PrintInt (int k) {
If (k == 0) {
Return 0;
}
Print (k);
PrintInt (k - 1);
}
Print 1 when the value of k is 0
PrintInt (int k) {
If (k == 0) {
Return 0;
}
Print (k);
PrintInt (k - 1);
}
Because the value of k is 0, nothing will print.

So now if we pass 5 in the printInt function, then five copies of the function will be created. That means, the number of functions that will pass, the number of times the function will be copied. And this is how the recurrence works.


Now see the implications of C programming

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
Void printInt (int k) {
    If (k == 0) {
        Return 0;
    }
    Printf ("% d \ n", k);
    PrintInt (k - 1);
}
Int main () {
    Int i;
    Printf ("Enter a number:");
    Scanf ("% d", & i);
    PrintInt (i);
    Return 0;
}
At first we wrote our printInt function in the program. Then we declare an integer inside the main function. It took input from user. Then passed the intridge in the printInt function. And printInt is the recursive function. That makes us work by calling ourselves.


Try using another simple program recursion. Such a number will take input from the user, then print the sum of all the numbers from 1 to the number. When the input is 4, the normal program will sum the sum as follows:
Sum (4) = 1 + 2 + 3 + 4
When there will be 5 inputs then
Sum (5) = 1 + 2 + 3 + 4 + 5
When the input will remain as 6 then
Sum (6) = 1 + 2 + 3 + 4 + 5 + 6

Which means:

Sum (6) = sum (5) + 6 [sum (5) = (1 + 2 + 3 + 4 + 5)]
Sum (5) = sum (4) + 5 [sum (4) = (1 + 2 + 3 + 4)]
Sum (4) = sum (3) + 4 [sum (3) = (1 + 2 + 3)]
Sum (3) = sum (2) + 3 [sum (2) = (1 + 2)]
Sum (2) = sum (1) + 2 [sum (1) = (1)]
Sum (1) = sum (0) + 1 [sum (0) = (0)]
Sum (0) = 0
The top steps are for 6 numbers. Now we can write an easy integer for sum of n numbers, sum (n) = sum (n-1) + n

When 0 is, you will print 0, and when n will, print the sum (n) = sum (n-1) + n.

And it's written in Sudo code

1
2
3
4
Sum (int n) {
    If (n == 0) return 0;
    Else return n + sum (n-1);
}
Applying in the C program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
Int sumFunc (int n) {
    If (n == 0)
        Return 0;
    Else
    Return n + sumFunc (n-1);
}
Int main () {
    Int n, sum;
    Printf ("Enter the value of n:");
    Scanf ("% d", & n);
    Sum = sumFunc (n);
    Printf ("Sum of n numbers:% d", sum);
    Return 0;
}
Another liberalization of recursion, I will now see how to remove Factorial using recurrence. At the beginning of this chapter we learned about factorial.


Factorial of 1
Factorial 1 * 2 = 2
Factory of 3 is 1 * 2 * 3 = 6.
Factory of 4 is 1 * 2 * 3 * 4 = 24.
Factory of 5: 1 * 2 * 3 * 4 * 5 = 120


For n numbers we can write as: factorial (n) = (n * factorial (n-1)); And since factorial (0) = 1, we can write a recursive function Sudo code for factorial:

1
2
3
4
5
6
Factorial (n) {
    If (n == 0)
    Return 1;
    Else
    Return (n * factorial (n-1));
}
Here the reason for returning 1 if n == 0 is the factorial (0) = 1.

If we find the value of factorial (3), the function will work like this:

Factorial (3) =
3 * factorial (2)
3 * 2 * factorial (1)
3 * 2 * 1 * factorial (0)
3 * 2 * 1 * 1
= 6

In programming, we can apply it as follows

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
Int factorial (int n) {
    If (n == 0)
    Return 1;
    Else
    Return (n * factorial (n-1));
}
Int main () {
    Int n, result;
    Printf ("Enter the value of n:");
    Scanf ("% d", & n);
    Result = factorial (n);
    Printf ("Factorial is:% d", result);
    Return 0;
}


No comments

Theme images by sebastian-julian. Powered by Blogger.