0% found this document useful (0 votes)
19 views

L5 Function

The document discusses functions in C programming. It defines what a function is, different types of functions like library functions and user-defined functions. It covers function declaration, definition, calling functions, passing arguments, return values, recursion, scope and lifetime of variables in functions, and different storage classes.

Uploaded by

Abror md Fayiaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

L5 Function

The document discusses functions in C programming. It defines what a function is, different types of functions like library functions and user-defined functions. It covers function declaration, definition, calling functions, passing arguments, return values, recursion, scope and lifetime of variables in functions, and different storage classes.

Uploaded by

Abror md Fayiaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Functions

Md. Ariful Islam Khandaker


Assistant Professor
IICT, KUET
Introduction
A function is a block of code that has a name and it has a property that it is
reusable i.e. it can be executed from as many different points in a C Program
as required and perform a definite task .

C functions can be classified into two categories, namely,


1. library functions and
2. user defined functions.

main( ) is an example of user-defined functions,


printf and scanf belong to the category of library functions.
Need for User defined Functions

 It facilitates top-down modular programming.

 The length of the source program can be reduced by using

functions at appropriate places.

It is easy to locate and isolate a faulty function for further investigations.

A function can be used by many other programs

3
THE FORM OF C FUNCTIONS
All functions have the form

Function-name(argument list with data types)


{
local variable declarations;
executable statement-1;
executable statement-2;
………
………
return(expression);
}

4
RETURN VALUES AND TYPES
The return statement can take the form:

return
or
return(expression);

Eg:if(x <= 0)

return(0);

else

return(1);

5
do nothing function

• A function that does nothing, may not include any


executable statements at all. For example-

do_nothing() { }

6
CALLING A FUNCTION
A function can be called by simply using the function name in the statement.

Eg:main()
{
int p;
p = multi(10,5);
printf(“%d \n”, p);
}

When the compiler executes a function call, the control is transferred to the
function multi(x,y).

7
Function Example

8
Function Declarations

9
Function Prototyping
When a program calls a function, the program control is transferred to the
called function. A called function performs a defined task and returns the
program control back to the main program.

10
Passing Function Arguments/Parameters

11
Call by Value

12
Call by Value continue…

13
Call by Reference

14
Call by Reference Example

15
CATEGORY OF FUNCTIONS
A function may belong to one of the following categories.
1) Functions with no arguments and no return values.
2) Functions with arguments and no return values.
3) Functions with arguments and return values.

NO ARGUMENTS AND NO RETURN VALUES

A function does not receive any data from the calling function. Similarly, it
does not return any value.

16
Function with no argument and no
return value
ARGUMENTS BUT NO RETURN VALUES

The nature of data communication between the calling function and the
called function with arguments but no return values is shown in the
diagram.

18
Function with argument and no
return value
ARGUMENTS WITH RETURN VALUES
Here there is a two-way data communication between the calling and the
called function.

20
Function with argument and return
value
RECURSION
When a function in turn calls another function a process of ‘chaining’ occurs.
Recursion is a special case of this process, where a function calls
itself.
Eg:1)
main()
{
printf(“Example for recursion”);
main();
}

22
RECURSION function example 1
unsigned int gcd(unsigned int a, unsigned int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
int main(){
int a,b;
scanf(“%d%d”,&a,&b);
printf(“%d”,gcd(a,b));
return 0;
}
23
RECURSION function example 2
int factorial(int n){
int fact;
if(n==1)
return 1;
else
fact= n * factorial(n-1);
return (fact);
}
int main(){
int a;
scanf(“%d”,&a);
printf(“%d”,factorial(a)); return 0;
}
24
FUNCTIONS WITH ARRAYS

To pass an array to a called function, it is sufficient to list the name of the


array, without any subscripts, and the size of the array as arguments.

Example:

void array_pass(int x[ ], int n){


----------------------------
----------------------------
}

main () {
int a[10], int n;
----------------
----------------
array_pass(a, n); 25
}
Scope and Lifetime of variables in function

• The scope of variable determines over what part(s) of the


program a variable is actually available for use(active).

• Longevity refers to the period during which a variable retains a


given value during execution of a program(alive).

26
Four Storage classes

• A variable in C can have any one of the four storage classes:


1. Automatic variables
2. External variables
3. Static variables
4. Register variables

27
Automatic variables or Local variable

28
External variables or global variables

29
Static variable
In C programming, when static variable is used in a program, it
causes only one copy that is to be shared by all functions.

30
Register variable
1. register keyword is used to define local variable.
2. Local variable are stored in register instead of RAM.
3. As variable is stored in register, the Maximum size of variable = Maximum
Size of Register
4. This is generally used for faster access.
5. Common use is “Counter“

31
Why we need Register Variable ?

32
Thank you

33

You might also like