How to create User Defined Functions in C, see example Perfect_Number() function used as UDF in C Program

How to create User Defined Functions in C, see example Perfect_Number() function used as UDF in C Program :-

Example : This is a program to find Perfect Numbers upto a limit using User Defined Function(UDF) in C Program Language :-

#include<stdio.h>
#include<conio.h>

int Perfect_Number(int); /* UDF function prototype declaration : int argument and int returns */

main()
{
int lmt,num;
clrscr();
printf("Enter a Limit :");
scanf("%d",&lmt);
for(num=5 ; num<=lmt ; num++)
{
/* function calls here with Actual Parameter num and returns either 1 or 0 and checks */
if(Perfect_Number(num)==1)
{
printf("\n%d",num);
}
}
getch();
}

/* Either the function should be defined out side the main() or it should be in one of the header files included. function definition with a Formal Parameter as n */

int Perfect_Number(int n)
{
int a,sm;
sm=0;
for(a=1 ; a<n ; a++)
{
if(n%a==0)
{
sm=sm+a;
}
}
if(sm==n)
return 1;
else
return 0;
}

/*
Run, and if we give limit as 500 then the output will be as below:
6
28
496
*/

Actual Parameter and Formal Parameters : Call by Value and Call by Reference

In computer programming, a parameter is a special kind of variable, used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are called arguments. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call can be assigned to the corresponding parameters.
Just as in standard mathematical usage, the argument is thus the actual value passed to a function, procedure, or routine, whereas the parameter is a reference to that value inside the implementation of the function.
In the most common case, call-by-value, a parameter acts within the subroutine as a local (isolated) copy of the argument, but in other cases, e.g. call-by-reference, the argument supplied by the caller can be affected by actions within the called subroutine.
The semantics for how parameters can be declared and how the arguments get passed to the parameters of subroutines are defined by the language, but the details of how this is represented in any particular computer system depend on the calling conventions of that system.


These two terms are sometimes loosely used interchangeably; in particular, "argument" is sometimes used in place of "parameter". Nevertheless, there is a difference. Properly, parameters appear in procedure definitions; arguments appear in procedure calls.
A parameter is an intrinsic property of the procedure, included in its definition. For example, in many languages, a minimal procedure to add two supplied integers together and calculate the sum total would need two parameters, one for each expected integer. In general, a procedure may be defined with any number of parameters, or no parameters at all. If a procedure has parameters, the part of its definition that specifies the parameters is called its parameter list.
By contrast, the arguments are the values actually supplied to the procedure when it is called. Unlike the parameters, which form an unchanging part of the procedure's definition, the arguments can, and often do, vary from call to call. Each time a procedure is called, the part of the procedure call that specifies the arguments is called the argument list.
Although parameters are also commonly referred to as arguments, arguments are more properly thought of as the actual values or references assigned to the parameter variables when the subroutine is called at run-time. When discussing code that is calling into a subroutine, any values or references passed into the subroutine are the arguments, and the place in the code where these values or references are given is the parameter list. When discussing the code inside the subroutine definition, the variables in the subroutine's parameter list are the parameters, while the values of the parameters at runtime are the arguments.
Many programmers use parameter and argument interchangeably, depending on context to distinguish the meaning. The term formal parameter refers to the variable as found in the function definition (parameter), while actual parameter refers to the actual value passed (argument).