5.1.4 Defining a Function
The function signature does not describe how the function performs the task assigned to it. Function definition does that. A function definition has the following general structure.
return_type functionname (data type varl, data_type var2,.., data type varN)
{
Body of the function
}
Body of the function is the set of statements which are executed in the function to perform the specified task. Just after the function's signature, the set of statements enclosed inside {} form the body of the function. Following example defines a function show Pangram () that does not take any input and does not return anything, but displays quick brown fox jumps over the lazy dog. On computer screen.
Example Code 5.1
As the above function does not return anything thus return type of the function is void.
Let's take another example of a function that takes as input two integer’s and returns the sum of both integers.
Example Code 5.2
Inside the function, return is a keyword that is used to return a value to the calling function.
Important Note:
A function cannot return more than one values, e.g. the following statement results in a compiler error. Return (4, 5);
Important Note:
There may be multiple return statements in a function but as soon as the first return statement is executed, the function call returns and further statements in the body of function are not executed.
Using a Function
We need to call a function, so that it performs the programmed task. Following is the general structure used to make a function call.
function_name(valuelj value2j...? valueN);
Example Code 5.3
We can see that the program starts its execution from main () function. When it encounters a function call (inside the rectangle), it transfers the control to called function. After executing the statements of called function, the control is transferred back to the calling function, .main in the above example. The following program inputs two numbers and displays their sum. The statement inside the rectangle in the following code includes a call to the add function defined in previous section.
Example Code 5.4
• In the function call n1 and n2 are arguments to the function add () discussed in
Example 5.2.
• Variable sum is declared to store the result returned from the function add (). • The variables passed as arguments are not altered by the function. The function makes a copy of the variables and all the modifications are made to that copy only.
• In the above example when n 1 and n2 are passed, the function makes copies of these variables. The variable is the copy of n 1 and the variable is the copy of n2.
Important Note:
The values passed to the function are called arguments, whereas variables in the function definition that receive these values are called parameters of the function. In the above example, values of variables nl and n2 are arguments to the function add(), whereas the variables x and y inside function add() are parameters of the function.
Timestamp
00:00 Video Intro
00:51 Defining a Function
01:33 General Structure
02:54 Example Code 5.1
04:16 Example Code 5.2
05:33 Important Note
06:26 Using a Function
06:35 General Structure
06:48 Example Code 5.3
08:35 Example Code 5.4
10:30 Arguments & Parameters
12:14 Outro