Вы находитесь на странице: 1из 5

Functions

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

Calling the function


A function can be accessed by specifying its name, followed by a list of arguments enclosed in parentheses
and separated by commas.
If the function call does not require any argument an empty pair of parentheses must be follow the name
of the function.
The argument appearing in the function call are referred to as actual arguments, in contrast to the formal
arguments that appear in the first line of the function definition.

//largest of three integer quantities {


#include<stdio.h> int a,b,c,d;
#include<conio.h> printf("Enter a=");
int maximum(int x,int y) scanf("%i",&a);
{ printf("Enter b=");
scanf("%i",&b);
printf("Enter c=");
int z; scanf("%i",&c);
z=(x>=y)?x:y; d=maximum(a,b);
return(z);
} printf("\nMaximum is = %d",maximum(c,d));
void main() getch();
}

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);
}

Passing arguments to functions

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

Returning values from functions


The return statement
Information is returned form the function to the calling portion of the program via the return statement. The
return statement also causes the program logic to the point form, which the function was accessed
Return expression is optional. if the expression is omitted the return statement simply causes control to revert
back to the calling portion of the program without transfer of information.
A function definition can include multiple return statements each containing a different expression. Function
that include multiple branches often require multiple returns
If a function does not return anything void specifier is used in the function declaration.
All the functions return type is by default is int i.e. a function returns an integer value if no type specifieres is
used in the function declaration

Defining a Function with an Argument: Formal Arguments


The function definition begins with this ANSI C declaration:

void n_char(char ch, int num)

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:

void dibs(int x, y, z) /* invalid function header */


void dubs(int x, int y, int z) /* valid function header */

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

In order to solve a problem recursively, two conditions must be satisfied.


First, the problem must be written in a recursive form
Second , the problem statement must include a stopping condition.
n!=1x2x3x4.xn, where n is the specified positive integer.
n!=nx(n-1)!
This is recursive statement of the problem in which the desired action is expressed in terms of a previous result.

/* recur.c -- recursion illustration */


printf("LEVEL %d\n", n); /* print #2
#include <stdio.h> */
void up_and_down(int); }
int main(void) The output looks like this:
{
up_and_down(1); Level 1
return 0; Level 2
} Level 3
void up_and_down(int n) Level 4
{ LEVEL 4
printf("Level %d\n", n); /* print #1 LEVEL 3
*/ LEVEL 2
if (n < 4) LEVEL 1
up_and_down(n+1);

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

/* rfactor.c -- uses recursion to else


calculate factorials */ printf("%d factorial = %ld\n",
#include <stdio.h> num, rfact(num));
long rfact(int n); printf("Enter a value in the range 0-
int main(void) 16 (q to quit):\n");
{ }
int num; return 0;
printf("This program calculates }
factorials.\n"); long rfact(int n) /* recursive function
printf("Enter a value in the range 0-16 */
(q to quit):\n"); {
while (scanf("%d", &num) == 1) long ans;
{ if (n > 0)
if (num < 0) ans= n * rfact(n-1);
printf("No negative numbers, else
please.\n"); ans = 1;
else if (num > 15) return ans;
printf("Keep input under 16.\n"); }
/* binary.c -- prints integer in binary printf("Enter an integer (q to
form */ quit):\n");
#include <stdio.h> }
void to_binary(int n); return 0;
int main(void) }
{ void to_binary(int n) /* recursive
int number; function */
printf("Enter an integer (q to {
quit):\n"); int r;
while (scanf("%d", &number) == 1) r = n % 2;
{ if (n >= 2)
printf("Binary equivalent: "); to_binary(n / 2);
to_binary(number); putchar(`0' + r);
putchar(`\n'); return;
}

/*power of input number recursive*/ getch();


#include<stdio.h> }
#include<conio.h> int pow(int n,int p)
main() {
{ if(p==0)
int num,power,ne; {
int pow(int,int); return(1);
clrscr(); }
printf("enter number="); else
scanf("%d",&num); {
printf("enter power="); return(n*pow(n,p-1));
scanf("%d",&power); }
ne=pow(num,power); }
printf("answer is =%d",ne);

Вам также может понравиться