Back to: C Tutorials For Beginners and Professionals
Introduction to Functions in C Programming Language
In this article, I will briefly introduce Functions in C programming language, mainly what function and functionality are and how many types of function declarations are present in C language. Please read our previous article discussing 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. Finally, it produced the output (technically, we called it the return type). For a 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 that is identified by the name add. I want to perform the additional operation. If you want to perform an addition operation, what minimum input must you pass? The minimum input is two integers. For example, take a calculator, and I want to perform an addition operation; a minimum of two numbers you have to press, or else it is impossible 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 pass 10 and 20, then 10 will go and store into x and 20 will go and store into y. In the processing logic, we add x and y and store the result in the z variable. After processing the information, we are returning that value stored in the z variable. The variable z is of integer type, so the return type of the add function is an integer.
So, finally, the definition of function is a block of instructions with an identity that takes input, processes the input, and produces 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, for the calculator, they already defined one functionality addition, i.e., the 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?
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 function, the function will belong to any 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 and NO RETURN FUNCTION
The first classification is no argument and no return values. I am writing one function. This function’s name is fun, and 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 a better understanding, please have a look at the below diagram. A void represents no value, and it means nothing. So, no arguments and no return values. This function is a block of instructions, and it does not take any arguments or return anything to anyone.
WITH ARGS and 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 a better understanding, please have a look at the diagram below. 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 void because no return values mean it does not return anything.
WITH ARGS and WITH RETURN FUNCTION
The third classification is a function with arguments and also with return values. For a 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, 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 is of integer data, so the return type is also an integer.
NO ARGS and WITH RETURN FUNCTION
No arguments and with return values means no arguments and with return values. For a better understanding, please have a look at the below diagram. See here. Function fun is having no arguments, which means void. We need to write void with return values. What is it 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 the 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 the 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. The function should have a definition, along with the definition function call is also important. If a function call is not there, it is useless.
How do we 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 a 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 the return type is also empty here. The function is not taking any input, so there is no need to pass any input, and it is not giving anything, so there is no need to get anything.
Calling With Args and No Return Function in C Language
In this case, what is it expecting?? The function definition expects an integer, so you must pass the integer. Suppose you want to perform a deposit operation; the bank personnel will expect some money from you. So then only they can process that information. Whenever you call this function, it expects an integer, so you must pass that integer. Any integer you can pass?? Here, I pass 10 so that it will go and be stored in 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 a 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 is it expecting?? It is expecting a character. In any programming language, we will represent characters using single quotes. If you want to pass a character, any character you can pass. Here, I am passing character ‘g’, which we are placing in a single quote. So, whenever you call this function ‘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, the right-side data always 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. The type is a void type. So, there is 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.
Summary:
In the next article, I am going to discuss Data Types in C Programming Language. In this article, I try to briefly introduce functions in the C Programming Language, and I hope you enjoy this article.
Image in the Calling With Args and With Return Function in C Language part of this article has an error.
Function with return can`t be void