[go: up one dir, main page]

0% found this document useful (0 votes)
7 views8 pages

9.Functions

The document explains the concept of functions in programming, detailing two types: library functions and user-defined functions. It covers function declaration, prototypes, local and global variables, return values, and the types of functions based on arguments and return values. Additionally, it discusses call by value and call by reference, as well as recursion with examples for better understanding.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views8 pages

9.Functions

The document explains the concept of functions in programming, detailing two types: library functions and user-defined functions. It covers function declaration, prototypes, local and global variables, return values, and the types of functions based on arguments and return values. Additionally, it discusses call by value and call by reference, as well as recursion with examples for better understanding.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Functions

A function is a self-contained block or a sub-program of one or more statements that


perform a special task when called.

There two types of functions

1) Library functions
2) User defined functions

1) Library functions-> The library functions are pre-defined set of functions. Their task
is limited. A user can not understand the internal working of these functions. The user
can only use the functions but can’t change or modify them. For example, sqrt(), cos(),
tan().

2) User defined functions-> The functions defined by the user according to his/her
requirement are called as User-defined functions. The user can modify the function
according to the requirement. The user certainly understands the internal working of the
function. The user has full scope to implement the his/her own ideas in the function. Thus
the set of such user-defined functions can be useful to another programmer.

Declaration of Function and Function prototypes

function_name(argument/parameter list)
argument declaration;
{
local variable declaration;
statement1;
statement2;
return(value);
}
void main()
{
……..
……..
abc(x,y,z); function call
actual argument

…………….
…………….
}
abc(l,k,j) function definition

formal argument
…………..
…………..
return(); return value
}

a) Actual Argument The arguments of calling functions are actual arguments. Variables
‘x’, ‘y’, and ‘z’ are actual arguments.
b) Formal Argument The arguments of called function are formal arguments. Variable
l,k and j are formal arguments.

WAP to show how user-defined function is called


void main()
{
int x=1,y=2,z;
z=add(x,y);
printf(“\n z=%d”,z);
}
add(a,b)
{
return(a+b);
}

Local and Global Variables

There are two kinds of variables. A) Local b) Global.


1) Local variable-> The local variables are defined within the body of the function or the
block. The variable defined is local to that function or block only. Other functions can not
access these variables. The compiler shows errors in case other functions try to access the
variables.

Example

value(int k, int m)
{
int r,t;
}

Here ‘r’ & ‘t’ are the local variables.

2) Global variables-> Global variables are defined outside the main() function. Multiple
functions can use them.

int b=10,c=5;
void main()
{
clrscr();
printf(“\nin main() b=%d c=%d”,b,c);
fun();
b++;
c--;
printf(“\nAgain in main() B=%d C=%d”,b,c);
getch();
}

fun()
{
b++;
c--;
printf(“\nIn fun() B=%d C=%d”,b,c);
}

Here variables ‘b’ & ‘c’ are defined outside the main() hence they are global.

Return value-> It is the outcome of the function. The result obtained by the function is
sent back to the calling function through the return statement. The return statement
returns one value per call. The value returned is collected by variable of the calling
function.

Function Prototypes

A function prototype declaration consists of the function’s return type,name and


arguments list. When the programmer defines the function, the definition of the function
must be same like its prototype declaration. If the programmer makes any mistake, the
compiler flags an error message. The function prototype declaration statement is always
terminated with semi-colon.
Ex-
a) float sum(float, int);
b) float sum(float x, int y);

The return statement

The user-defined function uses return statement to return the value to the calling function.
Exit from the called function to the calling function is done by the use of return
statement. When return statement is executed it always returns 1.

The return statement can be used in following ways.

a) return(expression)

example : return(a+b+c);
If such a statement is executed, the expression within the parenthesis is first solved and
the result obtained is returned.
b) A function may use one or more return statements. It is used when we want to return a
value depending upon certain conditions.

Types of functions

Depending upon the arguments present, return value sends the result back to the calling
function. Based on this, the functions are divided into four types.

1) Without arguments and return values.

Calling Function Analysis Called Function

void main() No arguments are passed. abc()


{ {
…………. No values are sent back …………………
………….. ………………….
abc(); ………………….
…………. }
…………
}

1) Neither the data is passed through the calling function nor the data is sent back from
the called function.
2) There is no data transfer between calling and the called functions.
3) The function is only executed and nothing is obtained.

Ex-
#include<stdio.h>
#include<conio.h>
void add();
void main()
{
clrscr();
add();
getch();
}
void add()
{
int a,b,sum;

printf(“\nenter two number :”);


scanf(“%d%d”,&a,&b);
sum=a+b;
printf(“\nsum=%d”,sum);
}
2) With arguments but without return values.

#include<stdio.h>
#include<conio.h>
void add(int a,int b);
void main()
{
int a,b;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
add(a,b);
getch();
}
void add(int a,int b)
{
int sum;
sum=a+b;
printf(“\nsum=%d”,sum);
}

3) With arguments and return values

#include<stdio.h>
#include<conio.h>
int add(int a,int b);
void main()
{
int a,b,s;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
s=add(a,b);
printf(“\nsum=%d”,s);
getch();
}
int add(int a,int b)
{
int sum;
sum=a+b;
return(sum);
}

4) Without arguments but with return values


#include<stdio.h>
#include<conio.h>
int add();
void main()
{
int s;
clrscr();
s=add();
printf(“\n sum=%d”,s);
getch();
}
int add()
{
int a,b,sum;
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
sum=a+b;
return(sum);
}

Call by value and reference

There are two ways in which we can pass arguments to the function.

1) Call by value
2) Call by reference

1) Call by value

In this type value of actual arguments are passed to the formal arguments and the
operation is done on the formal arguments. Any change made in the formal argument
does not effect the actual argument because formal arguments are photocopy of actual
arguments. Hence, when function is called by the call or by value method, it does not
affect the actual contents of the actual arguments. Changes made in the formal arguments
are local to the block of the called function. Once control returns back to the calling
function the changes made vanish.

#include<stdio.h>
#include<conio.h>
void swap(int a,int b);
void main()
{
int a,b;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
swap(a,b);
printf(“\n After swapping :”);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch();
}
void swap(int a,int b)
{
int c=a;
a=b;
b=c;
}

2) call by reference

In this type instead of passing values, addresses(reference) are passed. Function operates
on addresses rather than values. Here the formal arguments are pointers to the actual
arguments. In this type formal arguments point to the actual argument. Hence changes
made in the arguments are permanent.

#include<stdio.h>
#include<conio.h>
void swap(int *a,int *b);
void main()
{
int a,b;
clrscr();
printf(“\nenter two number :”);
scanf(“%d%d”,&a,&b);
swap(&a,&b);
printf(“\n After swapping :”);
printf(“\na=%d”,a);
printf(“\nb=%d”,b);
getch();
}
void swap(int *a,int *b)
{
int c=*a;
*a=*b;
*b=c;
}

Recursion

A function is called repetitively by itself is called recursion.


WAP to calculate factorial of entered number by using recursive function.

#include<stdio.h>
#include<conio.h>
int fact(int x);
void main()
{
int x,f;
clrscr();
printf(“\n Enter a number :”);
scanf(“%d”,&x);
f=fact(x);
printf(“\n Factorial=%d”,f);
getch();
}
int fact(int m)
{
int f=1;
if(m==1)
return(1);
else
f=m*fact(m-1);
return(f);
}

You might also like