You are on page 1of 68

USER-DEFINED FUNCTION

CONTENTS
 INTRODUCTION
 THE FORM OF C FUNCTIONS
 RETURNS VALUES AND THEIR TYPES
 CATEGORY OF FUNCTIONS
 RECURSION
INTRODUCTION

We have used functions in every program namely…….

main(), printf();, scanf();

C function can be classified into two categories

 Library functions
 user-defined functions
LIBRAY FUNCTION

A library function is a function that can be called by a


program to perform some task, but it is not part of the
program itself.

EXAMPLE

printf, scanf, sqrt, cos, pow etc


USER-DEFINED FUNCTION

User-defined functions has to be developed by the


user at the time of writing a program.

EXAMPLE

main, any valid identifier


NEED FOR USER-DEFINED FUNCTIONS

 If a program is divided into functional parts, then each


part may be independently coded and later combined
into single unit.

 These programs called ‘functions’ are easier to


understand, debug and test.
ADVANTAGES OF USER-DEFINED FUNCTION

The length of the source program can be reduced by


using functions at appropriate places.

It is easier to locate and isolate a faulty function for


further investigation.

A function may be used by many other programs.

It is facilitates top-down modular programming.


DEFINING A FUNCTION

A function definition has two principle components.

 The first line ( including argument declaration)

 body of the function


GENERAL FORMAT OF FUNCTION DEFINITION

data type name( type 1 arg 1, type 2 arg 2,…type n arg n)

 data type represents the data type of the item that is


return by the function.
 name represents the function name.
 type 1 arg 1, type 2 arg 2,…type n arg n represents
the data types of the argument arg 1 , arg 2.. .arg n.
ACCESSING A FUNCTION

A function can be accessed or called by specifying its name


followed by a list of arguments enclosed in parenthesis and
separated by commas.

If a function returns a value, the function access if written as

y=eee(a,b,c);
If a function does not return anything, then function access

eee(a,b,c);
KEY POINTS

 if the function call does not require any arguments, an


empty pair of parenthesis must be included.

y=eee();
 The arguments appearing in the function call are
referred as actual arguments.
 The arguments that appear in the first line of the
function definition is called formal argument.
EXAMPLE

Actual argument Formal argument

y=eee( a, b); int eee(int a,int b)


FUNCTION PROTOTYPE

A function prototype is a declaration in C of a


function, its name, parameters and return type.

GENERAL FORMAT

data type name( type 1 arg 1, type 2 arg 2,…type n arg n);
INFORMATION FROM FUNCTION PROTOTYPE

 Functions return type.

The number of its parameters.

 The type of its parameters.


KEY POINTS

 Function prototype are usually written at the


beginning of a program.
 A function prototype resembles the first line of a
function definition.
 Function prototype ends with a semicolon.
ADVANTAGE OF FUNTION PROTOTYPES

 Function prototypes inform the compiler about the


return type of a function.
 They allow the compiler to find and report illegal type
conversions.
 Prototypes also enable the compiler to report when the
number of arguments passed to a function is not same
as number of parameters declared by the function.
EXAMPLE

Function Function definition


prototype
int eee(int a, int b); int eee(int a, int b, int c)

Erros ocuurs because function prototype has two


arguments but function definition has three arguments.
THE FORM OF C FUNCTIONS

ALL FUNCTIONS HAVE THE FOLLOWING FORM


function_name(argument list) ANOTHER FORMAT
argument declaration; function_name(argument declaring)
{ {
local variable declarations; local variable declarations;
executable statement 1; executable statement 1;
executable statement 2; executable statement 2;
………………………….. …………………………..
return(expression);
return(expression);
}
}
KEY POINTS

 All parts are not essential.


 The argument list and its argument declaration parts
are optional.
 The declaration of local variables is required only
when any local variables are used in the function.
 A function can have any number of executable
stamens.
 The return statement is the mechanism for returning
a value to the calling function.
 Its absence indicates that no value is being returned
to the calling function.
ARGUMENT LIST

EXAMPLES OF FUNCTIONS WITH ARGUMENTS

eee(a,b,c)
mul(a,b)
add(x,y)
square(y)
EXAMPLE
All argument variables must be declared for their types
after the function header and before the opening of the
function body.

ANOTHER FORMAT
eee(a,b,c)
int a,b,c; eee(int a, int b, int c)
KEY POINTS

 The argument list contains valid variable names


separated by commas.
 The list must enclosed with parentheses.
 No semicolon follows the closing parenthesis.
 The argument variables receive values from the
calling function.
RETURNS VALUES AND THEIR
TYPES
GENERAL FORMAT

return;

or

return(expression);
KEY POINTS

 The first form return; does not return any value.

The second form return(expression); return with an


expression returns the value of the expression.

 A function may have more than one return statement.


EXAMPLE

mul( int x, int y){ if (x<=0)


int p; return(0);
p=x*y;
return (p); else
} return (1);
RETURN TYPE

We can force a function to return a particular type of


data by using a type specified in the function header.

double pro (x,y )

All functions by default return int type data.


KEY POINTS

 re-type specifies the type of data returned by the


function.
 If a function does not return a value, its return type
should be void.
 If a function does not use parameters, then its param-
list should be contain the keyword void.
 Of course you can call your functions by different
name.
CALLING A FUNCTION

main(){
int p;
p=mul(5,2);
printf(“%d”,p);}
calling function
KEY POINTS

A function that does not return any value may not


be used in expressions, but can be called to
perform certain task.

main(){
eee();
}
WRITE YOUR OWN FUNCTION
/* include header file here*/
/* function prototype here*/
main()
{
………}
ret-type1(param-list)
{
……}
.
.
ret-typen(param-list)
{
….}
CATEGORY OF A FUNCTION

 Category 1: Functions with no arguments and no


return values.
 Category 1: Functions with arguments and no return
values.
 Category 1: Functions with arguments and return
values.
NO ARGUMENTS AND NO RETURN VAULES

function1(){ no input function2(){


…………….; --------- ……….;
function2(); ……….;
………….; no output
} ----------- }

No data communication between function


KEY POINTS

 When a function has no arguments, it does not


receive any data from the calling function.

 When it does not return a value, the calling


function does not receive any data from the
called function.
PROGRAM: NO ARGUMENTS AND NO RETURN VAULES

#include<stdio.h>
#include<conio.h>
func1();
main(){
clrscr();
func1();
printf(“student”);
getch();
}
func1(){
printf(“eee”);
}
ARGUMENTS BUT NO RETURN VALUE

function1(){ values of arg. function2(b){


…………….; --------- ……….;
function2(a); ……….;
………….; no return value
} ----------- }

One way data communication


ARGUMENTS MATCHING BETWEEN CALLING AND
CALLED FUNCTION
actual argument
main() a1,a2,a3
{ formal argument
int a1, a2, a3; f1,f2,f3
function1( a1, a2, a3);
} calling function

function1(f1, f2, f3){


{
………….…. called function
}
ARGUMENTS WITH RETURN VALUES

function1(){ values of arg. function2(b){


…………….; --------- ……….;
function2(a); ……….;
………….; return value return(e);
} ----------- }

Two-way data communication


PROGRAM TO MULTIPLY TO INTEGER NUMBERS

int mul ( int a, int b); ANOTHER FORMAT


main (){ int mul ( int a, int b);
int p,a,b; main (){
p=mul(a,b); int p,a,b;
printf(“%d”, p); mul(a,b);
} printf(“%d”, mul(a,b) );
int mul (int a, int b){ }
int p; int mul (int a, int b){
p=a*b; int p;
return(p); p=a*b;
} return(p);
}
A MULTI-FUNCTION PROGRAM
void func1(void);
int main(void){
printf(“student”);  main() first call to printf(), printing
func1(); student
printf(“eee”);  Next func1() is called
 which prints of and return to main()
return 0;
 which prints eee
}
void func1(void){
printf(“of”);}
}
PROGRAM
int func(void);
int main (void){
int num;
num=func();
printf(“%d”,num);
return 0;
}
int func(void)
{
return 10;
}
A MULTI-FUNCTION PROGRAM
void func1(void);
void func1(void);
int main(void){  main() first calls func2()
func2();  func2() calls func1()
printf(“3”);  which prints 1 and return to func2()
return 0;  which prints 2 and return to main()
}  which prints 3
void func2(void){
func1();
printf(“2”);}
void func1(void){
printf(“1”);
}
RECURSION

Recursion is a process by which a function calls itself


repeatedly, until some specified condition has been
safisfied.

EXAMPLE recursion
recursion
main(){ recursion
printf(“recursion\n”); ……
main();
}
KEY POINTS

When a problem is solved recursively, two condition


must be satisfied.

 First the problem must be written in recursive


form.
 Second, the problem statement must include a
stopping condition.
PROGRAM: TO CALCULATE FACTORIAL N!
#include<stdio.h>
#include<conio.h>
long int fact( int n);
main(){
clrscr();
int n;
scanf(“%d”,&n);
printf(“n!=%d”,fact(n));
getch();}
long int fact(int n){
if (n<=1)
return(1);
else
return(n*fact(n-1))
;
}
User Defined Function
ASSIGNMENT

You might also like