FUNCTIONS(DATA AND VOID),RECURSION

The libraries that we use in C, consists of functions, for example in <stdio.h>, there exits functions, such as printf, scanf, fprintf, fscanf, etc. And each and every functions, are also made by coding, but in order to make it easier for us, the libraries are provided by the C language itself. If not, each time we want to print something, we have to code complicated stuffs , instead of just typing printf, to call the function that will execute the printing process.
BUT did you know? Besides using the function that is provided by the C library, you can also make your own function, in order to simplify things, so if you want to use a process many times, you don’t have to copy paste it all over and over  again every time it needs to be used, but instead you can just declare it as a function, so every time you need to use it, you just need to type the function in one line. And each function needs to be declared, basically there are data and void functions, so what is the difference?
“DATA” in Data Function means the data types, such as integer,bool,float,etc. So a function of each of these data types may be made, and the value that will be returned to caller of the function  will be based on the data types of the function.So, data function is used when you want a specific type of data to be returned to caller of the function, either it is main or another function. For example, if you want to return integer from the function, you can use
int function_name(parameter).” int means that you will return integer value from the function to the caller, same goes for float, bool, and other data types. So, if you return a float value in an integer function, it will be returned as integer in the caller.As for the function name, you can name it basically anything from your name, your girlfriend name, your pet’s name, etc. But it’s best to name a function with meaningful name, in order not to confuse yourself if you yourself write a complicated function. And parameter means that any data that you want to pass from the caller to be used in the function.
Meanwhile, as you know that void means empty, so void means that nothing is returned, and void won’t be related with any data types, basically anything within the void will stay local in the void and any value from the parameter that is processed or changed in the function, will not be changed in the caller, either it is another function or main, UNLESS if you use address(&) and pointer(*), any variables or values that is passed from main to the void function to be processed, will stay changed even after the void function. As for the void function, instead of using data types to declare the function, you just need to type “void function_name(parameter)” to declare the void function, besides that the same rules apply for the name and parameter.
Void functions are usually used if the process is finished in the void, including displaying the output, meanwhile  the data function are usually used when the value from the function needs to be used in the caller by returning its value and assigning it to a variable. But not only to display output, void function also tends to be used when you want to modify variable that already exists in the parameter, instead of making a new variable to assign the returned value.




Data Function Example(median)

So, the data type that I’ve chosen for this function is float, because the function that I made is to search for median, and median tends to be a decimal number if the number of numbers is even. As you can see, the value of median,which can be either round number if the number of numbers is odd,or decimal number if the number of numbers is even, from the median function is returned to main as float and assigned to the float variable named med in main, so the median value in the med variable can be directly printed from main.

This is how the output should look like, the 1st line is the number of numbers, and the 2nd line are the numbers, and the 3rd line are the median value
Void Function Example(median)


As you can see, all 3 pictures above are example for the void function
From the 1st picture, the output is displayed directly from the void function,and the function doesn’t return any value, so the process seems like it ends at the void function, because there isn’t anything left to do in main.
And for the 2nd and 3rd pictures, both of the output is displayed from main, so what is the difference?
        


The output from the 1st and 3rd picture can be seen at the top pifctur, meanwhile the ouput for the 2nd picture is the picture below it. As I said before, the value processed in the void function will stay local in the void function as in the 1st picture, and you’ll be able to print it directly from the void.
So, if you print it from the main after the void function, the value of the variable will be the same as before the function, because the process in the void function stays in the void and isn’t returned to main. UNLESS, if you use address and pointer for the variable that wants to be changed, address of the variable from main is passed to the void, so each time the variable is used in the void,it will point to its address from main, so you need to use pointer(*) everytime the variable wants to be changed and during its declaration in the void function. Only then, you will be able to display the output of the variable after being processed in the void function from main.

Meanwhile, recursion is just basically the functions that called itself, as long as in the function, there is a function with the same name, at least 1, that counts as a recursion


Luis Indracahya
2201758934
luis.indracahya@binus.ac.id
skyconnectiva.com

Komentar

Postingan populer dari blog ini

Algorithm And Programming-Repetition

Algorithm and programming-review