C Programming for Beginners : More on Functions

Suraj Das
4 min readNov 7, 2021

--

Calling a Function

Please pay attention to the comments in the program.

img 1
Calling the above function here
This is a function

Arguments and Parameters

The terms parameter and argument can be used for the same thing: information that are passed into a function.

A parameter is the variable listed inside the parentheses in the function definition.

Here is the proper syntax for defining a function :

Example :

Here, variable a and b are the parameters.

Lets see how the whole program looks like.

Adding 6 and 7 we get 13

At line 17, we called the add function.

While calling the function, we passed some information (x and y) with the function. These are arguments.

An argument is the variable listed inside the parentheses in the function call.

Note : The number of arguments should match the number of parameters. And their data types should be same too.

Here is another example :

Happy Birthday Elon!
You are now 100 years old.

Print vs Return

Notice these two programs for calculating the sum :

Program 1 :

3

Program 2 :

3

Breakdown of both the Programs

  • Both the programs does the same thing, that is, calculation of sum using functions.
  • The difference between the two programs is that the first program returns the data to a variable where the function is called, whereas, the second program prints the data when the function is called.
  • The data type of the returning value should match the data type of the function. And also the data type of the variable where the function is called should be same.
  • In Program 1, we returned an int type. So we specified the function type to be int. And the variable c is int too.
  • In Program 2, we did not want to return anything to any variable. That’s why we specified the data type of the function to be a void type.

Function Prototypes

Lets see what happens when you make some mistake while passing the arguments.

117

So it still gives us an output without showing us an error. And this is actually a bad thing as it will be hard for us to know to debug the code.

Here comes Function Prototypes!

A function prototype gives information to the compiler that the function may later be used in the program.

Ensures that calls to a function are made with the correct arguments.

You just have to write the data types of the arguments inside the parenthesis.

For example, for the function of the above code, the function prototype will be :

This will be placed before the main() function. And the add function will be moved to the bottom.

It won’t show any errors here too. But now we can easily navigate to the bug by reviewing the prototype. 👍

And that’s there!😊

Hope you enjoyed this blog.

Have any doubt ?
DM me on Instagram

Peace.

Index of C Programming lessons :

  1. Getting Started within 5 minutes
  2. Learning Fundamentals Made Easy
  3. Circumference and Area of Circle
  4. scanf() vs fgets()
  5. Hypotenuse Calculator
  6. Learn by Building a Calculator
  7. Functions
  8. More on Functions
  9. Length of an Array
  10. String Functions
  11. The While Loop

--

--