Ways to Define a Function
There are four types of programs available in the C language. They are:
1. Takes Nothing, Returns Nothing.
2. Takes Something, Returns Nothing.
3. Takes Nothing, Returns Something.
4. Takes Something, Returns Something.
1. Takes Nothing, Returns Nothing
Programming Example 1
Whenever we want to compile a program, the compiler has to know the meaning of every word written in a program which means it must declare every function or variable in the program. For this Function, Declaration is required. In Function Declaration, we write 3 things
- Return type
- Name of FM
But in the case of a predefined function, a declaration is written in the Header file. For example: clrscr () – -> Declaration is done in <stdio.h> file and function are defined in the library file. Turbo C++ 3.0 compiler automatically declares user-defined function. But we should declare a function. We only write the name and parameter passed in the argument in the calling function.
2. Takes Something, Returns Nothing
Programming Example 2
void add(int, int); //function declaration Globally
void add(int, int); //function declaration locally
printf ("Enter 2 numbers ");
scanf (" %d %d",&x,&y); // Actual arguments
void add(int x, int y) // Formal arguments
printf("sum is %d",c);
If we want to add the two values of two variables that are declared inside the main () and add this in a different function name adds (), we use the Takes Something Returns Nothing method. In this case, the value of x and y is passed to the add function when add () is calling in the main function. For this, it is called call by value.
Whenever we pass a value as an argument to a calling Function, then these arguments are called Actual Argument.
And in a Function definition, inside the parenthesis of a function, we declare the variable that will receive the value of the variable passed by the Calling Function, which is called a Formal Argument.
The name of the Actual Argument and Formal Argument variable may be the same because the compiler does not confuse in this case; it knows that variable x, y that is declared inside main () and x , y declared in add () are different variables.
In Calling Function, we just pass the value of variables that are declared inside main (). For this, we write add ( x, y) ;
3. Takes Nothing, Returns Something
Programming Example 3
Takes Nothing means, when even main () call a function to add (), that means it does not take any argument. Its means that add () take the value of variables from the user and perform the add.
Return something means add (); here, return the added result to the main () to use the keyword Return. When add () return the value, these value has to be kept in an int data type variable (s) that is declared in main (). Because here Return Type is int. That’s mean s= add () is replaced by, s= value of return.
For this, in the function definition of add (), here we have to mention the return type of add (), here is int. In the case of a return, we return only one value. So, return (a, b, c)-> is wrong. It means it only returns the value of c because it is written at least inside the parenthesis.
When any function returns any value, then it is to be understood that control also goes return to the calling function.
If we write any code after the return statement, that particular code has not been any effect on the program as the control shifts to the calling function.
Any function cannot access the variable declared by the other function. Because any variable that is declared inside a function is treated as a local variable.
So, in main (), we have to print the value of s instead of c because c is the variable that is declared inside the add ().
4. Takes Something, Returns Something
Programming Example 4
Takes Something Returns Nothing program is the same as the Takes Nothing Returns Something; the only difference is that, as it is by takes, something is in nature, so the value of two numbers has to be passed in the calling function.
That means the user takes the value from the keyboard used by the main (). That value is passed to the add ().
So, it is on the type of call by value example. Otherwise, the return procedure is the same as the Takes Nothing, Returns Something Procedure.
Learning about different types of functions in C, we have concluded that every type of function gives the same result. But each of them has a different mechanism. Each of them is needed according to the requirement of the program. So we have to know each type of function.