Академический Документы
Профессиональный Документы
Культура Документы
A function is a self-contained block of executable code that can be called from any other function.
A special function name is main( ): the function with this name is the first one to run when the program
starts. All other functions are subroutines of the main( ) function (or otherwise dependent procedures, such
as call-back functions), and can have any names you wish.
Every function is defined exactly once. A program can declare and call a function as many times as necessary.
Generally a function will process information that is passed to it from the calling portion of the program and
return a single value.
Information is passed to the function via special identifiers called arguments and returned via the return
statement.
#include<stdio.h> {
#include<conio.h> char lower,upper;
char upperc(char c1) clrscr();
{ printf("Enter lowercase character=");
char c2; scanf("%c",&lower);
c2=(c1>='a'&&c1<='z')?('A'+c1-'a'):c1; upper=upperc(lower);
return(c2); printf("\nUppercase equ is = %c",upper);
} getch();
void main() }
Function Definitions
The definition of a function consists of a function head (or the declarator), and a function block . The
function head specifies the name of the function, the type of its return value, and the types and names of
its parameters, if any. The statements in the function block specify what the function does.
Data-type name(type argu)
In the function head, name is the function's name, while type consists of at least one type specifier, which
defines the type of the function's return value.
The return type may be void or any object type, except array types. Furthermore, type may include the
function specifier inline, and/or one of the storage class specifiers extern and static.
A function cannot return a function or an array. However, you can define a function that returns a pointer
to a function or a pointer to an array.
The parameter declarations are contained in a comma-separated list of declarations of the function's
parameters. If the function has no parameters, this list is either empty or contains merely the word void
Function prototypes
Function prototypes are usually written at the beginning of a program.
By declaring a function before using it, you inform the compiler of its type: in other words, a declaration
describes a function's interface.
A declaration must indicate at least the type of the function's return value.
The data types of the actual arguments must conform to the data types of the arguments within the
prototype.
The prototype provides the compiler with important information about the return type and parameters.
long int fact(int n);
main()
{
int n;
printf(n=);scanf(%d,&n);
printf(fac=%d,fact(n));
}
long int factorial(int n)
{
int i
long int prod=1;
if(n>1)
for(i=2;i<=n;++i)
prod*=i;
return(prod);
}
When a single value is passed to a function via an actual argument, the value of the actual argument is copied
into the function.
Therefore the value of the corresponding formal argument can be altered within the function, but the value of
the actual argument within the calling routine will not change
//alters the value of its arguments modify(a);
printf("\nfrom main after calling function a=%i",a);
#include<stdio.h> getch();
#include<conio.h> }
void modify(int a); void modify(int a)
{
void main() a*=3;
{ printf("\nFrom the function a=%i",a);
int a=2; return;
printf("\nbefore calling function in main()a=%i }
\n",a);
Passing constants
Passing variable
Passing by value
This line informs the compiler that n_char() uses two arguments called ch and num, that ch is type char, and that num is type int.
Both the ch and num variables are called formal arguments. Like variables defined inside the function, formal arguments are local
variables, private to the function. That means you don't have to worry about duplicating variable names used in other functions. These
variables will be assigned values each time the function is called.
Note that the ANSI C form requires that each variable be preceded by its type. That is, unlike the case with regular declarations, you
can't use a list of variables of the same type:
Recursion
Recursion is a process by which a function calls itself repeatedly, until some specified condition has been
satisfied. The process is used for repetitive computations in which each action is stated in terms of a previous
result
When a recursive program is executed, the recursive function calls are not executed immediately. Rather they are
placed on a stack until the condition that terminates the recursion is encountered. The function calls are then
executed in reverse order, as they are popped off the stack