Up to this point, we are fluent in using printf() and scanf() functions. When they are called they do a specific task for us (printing something on the screen or taking input from a user). How do they do it? We don’t know and we are lucky enough that the developers write the code for the functionalities of such functions. That’s why they are known as built-in functions. Here in this post, we are willing to make some functions that we know how they will work, or in other words, we are going to write some user-defined functions.
To write user-defined functions, just keep in mind 3 points:
- Function name: Give a name to your function, for example, my_function().
- Function input: Think of your function as a factory. As a factory takes some input, your function may take ‘n’ inputs. (n>=0)
- Function output: Again think of your function as a factory. As a factory generates some output, your function may produce as well.
- Function call: A function is lazy as usual. It does not do the work if it is not called. So, to make it do the work, you just need to call it.
Based on the input and output, a user-defined function can be divided into 4 categories:
- Function with no inputs and no output
- Function with inputs but no output
- Function with no inputs but an output
- Function with inputs and an output
Function with no inputs and no output
Take a look at this code.
![](https://zeroneincore.com/wp-content/uploads/2024/07/Screenshot-1531-1024x576.png)
Here in this code, we have defined a function from lines 3-6.
At line 3, we have written void my_function().
why void? Because the function returns no values.
Why my_function()? This is just the name of our function. And the parentheses is blank inside because our function does not take any input.
Line 4-6 is the scope of our function. Here in this scope we write what tasks the function is supposed to do. In this case, the function is only given a task to print a statement.
If we hit the run button, what is the output?
The output is blank. Why?
Because you did not call the function. So take a look at the updated code with function call:
![](https://zeroneincore.com/wp-content/uploads/2024/07/Screenshot-1532-1024x576.png)
![](https://zeroneincore.com/wp-content/uploads/2024/07/Screenshot-1533.png)
So now the function is running perfectly. Apart from that, always keep in mind the flow diagram of a function. Take a look at this
![](https://zeroneincore.com/wp-content/uploads/2024/07/IMG_0300-998x1024.jpg)
When a function finishes it’s task, (it encounters the closing curly bracket }) it returns to the next line from where the function was called.
Thank you