Defining Calling and return Statement of a Function
For Complete YouTube Video: Click Here
In this class, we will try to understand Defining Calling and return statement of a Function.
We have already covered the basic concept of function.
Table of Contents
Defining Calling and return statement of a Function
Defining a Function
We will try to understand the concept of defining a function using the example shown in the image below.
Defining a function means writing all the statements that a ‘function’ has to do.
In our example, the sum is the name of the function with return type int.
Return type in the function definition states that after doing all the functionality by the function, the value returned by the ‘function’ is of integer type.
After the function name open and closed, parentheses are used.
Within those parentheses, parameters of the function are provided.
The parameters are the members of the functions.
They have to define their types, as shown in the image above.
The two parameters, x, and y are of integer type.
The body of the function starts with open and closed curly braces.
Within those parameters, we have to define all the statements required for the functionality to be done.
return Statement
At the end of the function definition, we have to mention the return statement.
The return statement in our example is returning the value of z.
The value of z is returned to the function call.
Calling Function
In the above example, all the calling functions are marked in red.
A function call is a point where we need specific functionality to be done.
Within the open and closed braces of the function call, we will provide the arguments.
The arguments of the function call are the values upon which the action has to be done.
The function call arguments are transferred and will be stored in the parameters of the function definition.
In the first function call, values of a and b are stored in the parameters x and y of the function definition.
The same program can also written in a different way as shown in the image below.
In the above example, the function call is made within the printf() function.
If there is no need to remember the return values of a function then such kind of declarations can be made.