Introduction to Functions in C Programming Language
In this article, I am going to give a brief introduction to functions in C Programming Language, mainly what is function and functionality and how many types of function declarations present in C language. Please read our previous article where we discussed Variables in C Programming Language.
What is a function?
A function is a block of instructions (here we can write n number of valid instructions) having identity (the name is mandatory) and taking inputs (technically called as argument list) and processing the input and finally, it produced the output (technically we called it as return type). For better understanding, please have a look at the following diagram.
Understanding Function with an Example in C Language:
Let us see an example to get more clarity on function. Please have a look at the following diagram. Just consider the block of instructions and is identified by the name add. I want to perform the additional operation. If you want to perform an addition operation, what is the minimum input you have to pass, the minimum input is two integers? For example, take a calculator, I want to perform an addition operation, minimum of two numbers you have to press, or else it is not possible to perform an addition operation. So here are two numbers we are collecting into two variables i.e. int x and int y. Suppose if you type 10 and 20 then 10 will go and store into x and 20 will go and store into y. And in the processing logic, we are adding x and y and storing the result into the z variable. After processing the information, we are returning that value store in the z variable. The variable z is of integer type, so the return type of the add function is an integer.
So, function means what, it is doing some tasks. The marker is functioning means what, it is doing something. What the marker is doing, it is writing. Mobile is functioning means what, so many functionalities are there. We are calling, we can message, we can play games, we browse the internet, etc. A person functioning means what, a person can teach, a person can walk, a person can speak, a person can eat, a person can play, etc. So, all these are coming under functionalities. So, what is a function means, is performing a particular task. So, in our example, the add function performing the addition of two numbers task,
So, finally, the definition of function is, block of instructions having an identity that is taking input, processing the input, and producing the output.
One more important point that you need to add to function is, only if you are writing the definition of a function, is no use. In every program along with the function definition, one more thing is also important i.e. function call.
For example, calculator, they already defined one functionality addition i.e. plus button they are given. In the entire lifetime of the calculator, if no one is using that identity nothing but no one is using that plus button, then what is the use of that. If one object is there nothing but one physical thing is there and it is having functionality means everyone should use that functionality at least once in its lifetime.
What is functionality?
So simply we can understand what is functionality means just consider one electronic device and the best example is television which we use in our daily life. In the background, one program is there which is running with the help of the program. They have already written one program, how they will write the program means to on the television one functionality is required, it is power on and to off the television another functionality is required i.e. power off. To change the channel functionality is there. And mostly one more functionality we are using every day is volume, so, volume is the functionality they have given. The logic they are already written. But see only functionality is of no use. If you just look at the TV, will it start automatically? Impossible right. Will it change the channels automatically? absolutely not. Along with the functionality i.e. along with the function definition, calling is also very important.
Another person nothing but another program. suppose take one person and the person is using the remote control. The remote controller is also a program and the person is also a program and the television is also a program. And they have to call the functions. One is on function, another off function as well as channel change function. They will do for example channel + + channel – -, volume + + volume – -, etc. So. another program (a person) should call the functionality of television from another place (remote controller). So, whenever they click on the ON function, then the corresponding logic executes and the system will on, and whenever they click on the OFF button then automatically off functionality executes. So, not only the definition is important, but function calling is also important. For better understanding please have a look at the below image.
Classification of Functions in C Language:
Generally, only four method classifications are present, whatever the programming language you take. If you look at one particular method or one particular function, the function will belong to any one of these four classifications. There are generally 4 types namely:
- NO ARGS & NO RETURN
- WITH ARGS & NO RETURN
- WITH ARGS & WITH RETURN
- NO ARGS & WITH RETURN
NO ARGS & NO RETURN FUNCTION
The first classification is no argument and no return values. I am writing one function, this function name is fun, it is a block of instructions. The function is not taking anything i.e. taking no arguments. In this classification, there are no arguments, so here we need to write no arguments and no return values. For better understanding, please have a look at the below diagram. Void is representing no value and it means nothing. So, no arguments and no return values. This function is a block of instructions and it is not taking any arguments and not returning anything to anyone.
WITH ARGS & NO RETURN FUNCTION
Next comes the second classification, with arguments and no return values. We need to pass the arguments of values integers, float, double character, strings, etc. All these come under input types. For better understanding please have a look at the below diagram. Here, simply I am passing one integer to collect that. It will process that input with the predefined logic and finally, it will return the output. Output is a void because no return values mean it is not returning anything.
WITH ARGS & WITH RETURN FUNCTION
The third classification is a function with arguments and also with return values. For better understanding, please have a look at the below image. Here, the function is taking arguments and we are passing characters. Any number of arguments we can pass. It has no limitations and depends on your application requirement; the function can take any number of arguments. Here, simply I am passing only one argument i.e. of type character and here it has return values. So, the return statement is mandatory. Return statement 13 means, it is returning value 13 and it is of integer data, so the return type is also an integer.
NO ARGS & WITH RETURN FUNCTION
No arguments and with return values means no arguments and with return values. For better understanding, please have a look at the below diagram. See here function fun is having no arguments means void. We need to write void with return values. What it is returning? Suppose here it is returning the value 34.56. it is of type float type or double. So here the return type is also float.
There is no such restriction that, what type of data we are taking, has to return the same type of data. For example, in the withdrawal operation in Bank. The input is a just pin number and how much amount you want to withdraw but the output is the amount of money. Take a deposit function input is the money and output is “Deposit Successful”. So, there is no relation between input and output. Any function can take different types of inputs and other types of outputs.
If only the function definitions are present then it is of no use. If functionality is there someone should call that functionality. If only functionality is there, it is of no use someone should call it from another place. The function should have a definition, along with the definition function call is also important. If a function call is not there then it is of no use.
How to call these functions?
Depends on the classification.
Calling No Args and No Return Function in C Language
Function calling is always a single statement. A single statement means it must end with a semicolon. For better understanding please have a look at the below image. Whenever you call this function, is it expecting anything?? Is it taking any input?? The answer is No. So, no need to pass anything here it is empty. Is it giving anything?? No. It is not returning anything, so here the return type is also empty. The function is not taking any input so no need to pass any input and it is not giving anything so no need to get anything.
Calling With Args and No Return Function in C Language
In this case, what it is expecting?? The function definition is expecting an integer, so you have to pass the integer. Suppose if you want to perform a deposit operation, the bank personnel will expect some amount of money from you. So then only they can process that information. Whenever you call this function, it is expecting an integer, so you have to pass that integer. Any integer you can pass?? Here, I am passing 10, so that it will go and store into x. Now x value is 10 right. Then it will process 10 but is it giving anything?? No. It’s not giving anything so no need to collect anything. For better understanding, please have a look at the following image.
Calling With Args and With Return Function in C Language
Please have a look at the below diagram for a better understanding. In this case, what it is expecting?? It is expecting a character. In any programming language, we will represent characters using single quotes. If you want to pass character, any character you can pass. Here I am passing character ‘g’, we are placing in a single quote. So, whenever you call this function that ‘g’ will go and store into variable x. This is the value it will hold, and the output will be 13. We should collect them into another variable. Here it is returning 13, 13 is of type integer. So, we are collecting that result into an integer type variable only. For assignment operators always right-side data executes first. We are calling the function and we are passing the input character and it is returning something.
Calling No Arg and With Return Function in C Language
For better understanding please have a look at the below diagram. Suppose we are calling the function fun. Is it expecting anything?? No. Type is a void type. So, no need to pass anything to anyone but here it is expecting something i.e. it is returning 34.56 of float type. So, we should collect that into a variable of float type. Whether you are passing some values and someone is giving some output, we should collect that into a variable. But here we need to declare a variable of type depending upon the return type.
In the next article, I am going to discuss Data Types in C Programming Language. Here, in this article, I try to give a brief introduction to functions in C Programming Language and I hope you enjoy this Functions in C Programming Language article.