0% found this document useful (0 votes)
21 views47 pages

CP Module-3 2023-24 PPT

Module 3 covers the concept of functions in C programming, including their definition, types, advantages, and how to create user-defined functions. It explains function prototypes, definitions, calls, recursion, and the differences between call by value and call by reference. Additionally, it discusses storage classes in C, detailing automatic, static, register, and external storage classes.

Uploaded by

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

CP Module-3 2023-24 PPT

Module 3 covers the concept of functions in C programming, including their definition, types, advantages, and how to create user-defined functions. It explains function prototypes, definitions, calls, recursion, and the differences between call by value and call by reference. Additionally, it discusses storage classes in C, detailing automatic, static, register, and external storage classes.

Uploaded by

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

Module 3

● Introduction to Function.
● Function prototype, Function definition, accessing a function
and parameter passing.
● Recursion.
What is Function?

- A function is a self contained block of code/statement that performs a specific task.


A function are sub program .It is not a complete program whereas it is a part of program.
It has a name for identification.
- Suppose, you need to create a program to create a circle and color it. You can create two
functions to solve this problem:
● create a circle function
● create a color function

Dividing a complex problem into smaller chunks makes our program easy to understand
and reuse.

Types of function
There are two types of function in C programming:
● Standard library functions
● User-defined functions
Advantages of using Functions

1. By using functions, we can avoid rewriting same logic/code again and


again in a program.
2. We can call C functions any number of times in a program and from
any place in a program.
3. We can track a large C program easily when it is divided into multiple
functions.
4. Reusability is the main achievement of C functions.
Benefits of functions
1. Function to proper utilization of memory
2. Once function defined can be used many times
3. Avoid repetition of code
4. Divide complex problem into simpler ones
5. Increase program readability
6. Reduce changes of error
Standard Library Functions/Built-In Functions

The standard library functions are built-in functions in C programming.


These functions are defined in header files. For example,
1. math.h:- contain Functions required for mathematical operations like:
sqrt( ), abs( ), pow( ), ceil( ), floor( ), fabs() etc.

2. string.h:- contains functions required for string manipulation like:


strlen( ), strcpy( ), strcmp( ), strrev( ) etc.

3. stdio.h:- printf( ), scanf( ) etc.

4. conio.h:- getch( ), clrscr( ) etc.


User defined Functions
- We can also create functions as per our need. Such functions created
by the user are known as user-defined functions.

It consist of 3 part
- For writing user defined functions following things are required.

1. Function prototype/Function declaration.

2. Function definition.

3. Function call.
Function prototype:

In C all functions must be declared before they are used in program. This is achieved by functions
prototyping. The prototyping is a declaration that defines the arguments passed to the function and the
type of the value returned by the function. It has the form.

return type function_ name (type arg1, type arg2,........type argn);

Here returntype specifies type of value returned by function. function_name is any valid

variable name other than keyword. The arg1, arg2------ argn are n arguments. Before names of

argument the type of argument is mentioned.

The use of argument names is optional, however writing

their type is mandatory.

For example, we may write function prototype as:

int factorial(int n);

int GCD(int, int);


User defined Functions
- Function declaration:- A function declaration tells the compiler about a
function name and how to call the function.
- Function call:- While creating a C function, you give a definition of what the
function has to do. To use a function, you will have to call that function to
perform the defined task. Function can be called from anywhere in the
program. The parameter list must not differ in function calling and function
declaration. We must pass the same number of functions as it is declared in
the function declaration.
- Function definition:- It contains the actual statements which are to be
executed. It is the most important aspect to which the control comes when the
function is called.
Defining a function:

returntype function_name (type arg1, -------,type argn)

local variable declarations ;

executable statement1;

executable statement2;

------------

return (expression);

}
Function Call:

A function can be called in main() by writing the name of the function.

For example, we may write statement

Bill = total _cost (number , price );

void main( ) {

int sqr (int x); function prototype

int m,t=10;

m=sqr(t); function sqr is called (call by value)

printf(“%d”,m) }

int sqr(int x) {

x=x*x;

return (x);
The return statement:

When a function is called, the control of execution gets transferred to function.

For example we may have a plane return as:

return;

The plane return does not return any value but it only returns the control of execution
to the

calling function.

return(expression);
User defined Functions
Examples on Functions or types
Example for Function without argument and without return value
Example for Function without argument and with return value

Example for Function with argument and without return value

Example for Function with argument and with return value

Argument - is the actual value of the parameter that gets passed to


the function.
Examples on Functions
Example for Function without argument and without return value:-
Program to add two numbers using function.

Output
Examples on Functions
Example for Function without argument and with return value:-
Program to add two numbers using function.

Output
Examples on Functions
Example for Function with argument and without return value:-

Program to add two numbers using function.

Output
Examples on Functions
Example for Function with argument and with return value:-

Program to add two numbers using function.

Output
Examples on Functions
Program to calculate factorial of a number

using function:

Output
#include<stdio.h>
int findFactorial(int); WAP to find out value
void main() { of BIO using function,
where BIO is
int i,BIO,n,r;
defined as BIO= n!/(r!
printf("Enter the value of n and r: "); (n-r)!), n and r are
scanf("%d%d",&n,&r); natural numbers.
Page no 28
BIO = findFactorial(n)/( findFactorial(r)* findFactorial(n-r));
printf("Value of BIO is %d:",BIO);
getch( ); }
int findFactorial(int num) {
int i,f=1;
for(i=1;i<=num;i++)
f=f*i;
return(f); }
Call by value and Call by reference in C
There are two methods to pass the data into the function in C language, i.e., call by value and
call by reference.

Call by value in C:-

○ In call by value method, the value of the actual parameters is copied into the formal
parameters. In other words, we can say that the value of the variable is used in the function
call in the call by value method.
○ In call by value method, we can not modify the value of the actual parameter by the formal
parameter.
○ In call by value, different memory is allocated for actual and formal parameters since the
value of the actual parameter is copied into the formal parameter.
○ The actual parameter is the argument which is used in the function call whereas formal
parameter is the argument which is used in the function definition.
Call by value
Call by value
Call by value and Call by reference in C

Call by value in C:- Program to swap value of two numbers


Call by value and Call by reference in C

Call by value in C:- Program to swap the values of two numbers

Output:-
Call by value and Call by reference in C

Call by reference in C:-

○ In call by reference, the address of the variable is passed into the function
call as the actual parameter.
○ The value of the actual parameters can be modified by changing the formal
parameters since the address of the actual parameters is passed.
○ In call by reference, the memory allocation is similar for both formal
parameters and actual parameters. All the operations in the function are
performed on the value stored at the address of the actual parameters, and
the modified value gets stored at the same address.
Call by reference
Call by reference
Call by value and Call by reference in C
Call by reference in C:- Program to swap the values of two numbers
Call by value and Call by reference in C

Call by reference in C:- Program to swap the values of two numbers


Output:-
Difference between Call by Value and Call by reference
Examples on Functions
Practice Programs:-
- WAP in C to calculate average of 5 numbers using function.
- WAP in C to check whether number is even or odd using function.
- WAP in C to calculate factorial of a given number using function.
- WAP in C to find GCD and LCM of two integers using function.
- WAP in C to find the number entered by user is Armstrong number
or not using function.
Program to calculate
factorial of a given number
using function.

Output
Recursion:
- When a function calls itself directly or indirectly is called recursion.
Generally a function calls another function in that case control goes to a
function executes it and comes back.
- When a function calls itself then control goes to same function and
executes it again.
- In recursion a function can starts with a new value every time. It is very
compact and powerful.
- It must be conditional otherwise it may enter into an infinite loop.
Recursion:
Program to calculate factorial of a
given number using recursive
function.
Program to find GCD using recursion
Output:- enter p & q:20 40

result=20

Greatest common divisior

20 40

1,2,4,5,10,20 1,2,4,5,10,20,40 =20

If p=0 then b

If q=0 then a otherwise (b, as%b)

(40,20%40)
Storage Classes

- Storage Classes are used to describe the features of a


variable/function. Storage class gives four different information
about variable.
- C language uses 4 storage classes, namely:

Storage class keyword


Automatic storage class auto

Register storage class register

Static storage class static

External storage class extern


Storage Classes

Storage class gives four different information about variable.

1) Location: Which memory will be allocated Main memory Or CPU register?

2) Default value: What will be default value of variable?

3) Scope/ Visibility: Where variable will be accessible/visible?

4) Lifetime: When memory will be allocated and destroyed to the variable?

Note:- The Garbage value is a random value at an address in the


memory of a computer.
Storage Classes

Zero
1. Automatic storage class

○ Automatic variables are allocated memory automatically at runtime.


○ The visibility of the automatic variables is limited to the block in which they
are defined.
○ The scope of the automatic variables is limited to the block in which they
are defined.The automatic variables are initialized to garbage by default.
○ The memory assigned to automatic variables gets freed upon exiting from
the block.
○ The keyword used for defining automatic variables is auto.
○ Every local variable is automatic in C by default.
1. Automatic storage class
Example:-
2. Static storage class

○ The variables defined as static specifier can hold their value between the
multiple function calls.
○ Static local variables are visible only to the function or the block in which
they are defined.
○ A same static variable can be declared many times but can be assigned at
only one time.
○ Default initial value of the static integral variable is 0 otherwise null.
○ The visibility of the static global variable is limited to the file in which it
has declared.
○ The keyword used to define static variable is static.
2. Static storage class
Example:-

Output
3. Register storage class
○ The variables defined as the register is allocated the memory into the CPU registers
depending upon the size of the memory remaining in the CPU.
○ We can not dereference the register variables, i.e., we can not use &operator for the
register variable.
○ The access time of the register variables is faster than the automatic variables.
○ The initial default value of the register local variables is 0.
○ The register keyword is used for the variable which should be stored in the CPU register.
However, it is compiler?s choice whether or not; the variables can be stored in the
register.
○ We can store pointers into the register, i.e., a register can store the address of a
variable.
○ Static variables can not be stored into the register since we can not use more than one
3. Register storage class

Example:-

Output will be ‘0’


4. External Storage Class
○ The external storage class is used to tell the compiler that the variable defined as
extern is declared with an external linkage elsewhere in the program.
○ The variables declared as extern are not allocated any memory. It is only declaration
and intended to specify that the variable is declared elsewhere in the program.
○ The default initial value of external integral type is 0 otherwise null.
○ We can only initialize the extern variable globally, i.e., we can not initialize the
external variable within any block or method.
○ An external variable can be declared many times but can be initialized at only once.
○ If a variable is declared as external then the compiler searches for that variable to be
initialized somewhere in the program which may be extern or static. If it is not, then
the compiler will show an error.
4. External Storage Class

Example:-

Output:- 0
4. External Storage Class

Example:-

Output:- 20

You might also like