Functions in C: Functions with No Inputs and No Return Value

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:

  1. Function name: Give a name to your function, for example, my_function().
  2. Function input: Think of your function as a factory. As a factory takes some input, your function may take ‘n’ inputs. (n>=0)
  3. Function output: Again think of your function as a factory. As a factory generates some output, your function may produce as well.
  4. 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:

  1. Function with no inputs and no output
  2. Function with inputs but no output
  3. Function with no inputs but an output
  4. Function with inputs and an output

Function with no inputs and no output

Take a look at this code. 

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:

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

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