Functions in C: Functions with Inputs but No Return Values

Okay, let’s assume we need to create a function for adding two numbers. We want to add two numbers using a user-defined function.

Take a look at 7. Our add() function will do the summation task on 2 inputs. On which inputs? On a and b, that’s why we put them in the first parentheses of the function add(a,b) so that the function can understand these 2 integer values are the inputs.

But up to this point the program does not know how this add() works. So let’s create a function definition a body of a function.

As our function needs to work with 2 integer values, we need to inform this in the function body. That’s what we did at line 3 inside the parentheses: declaring num1 and num2 as integers. Now the a and b will be treated as num1 and num2 respectively, inside add() function body/block (from lines 4 to 7)

The void keyword at line 3 is used because the function does not return any value to the scope (here main() ) from where it was called.

From line 4 to 7, we just did the summation and print the values

When the program execution encounters the } at line 7, it returns to the next line from where it was called.

Want to have the best mouse for programing? You can check it out here.

So that’s all. Thank you.