0% found this document useful (0 votes)
3 views29 pages

Lecture Fucntions

The document provides an overview of user-defined functions in programming, detailing their types, advantages, and components. It explains the importance of function prototypes, return statements, and different function calls, including by value and by reference. Additionally, it discusses variable types such as automatic, external, static, and register variables, highlighting their scopes and lifetimes.

Uploaded by

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

Lecture Fucntions

The document provides an overview of user-defined functions in programming, detailing their types, advantages, and components. It explains the importance of function prototypes, return statements, and different function calls, including by value and by reference. Additionally, it discusses variable types such as automatic, external, static, and register variables, highlighting their scopes and lifetimes.

Uploaded by

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

1

User-Defined Functions
2
It is necessary to declare
the prototype of every
function that we intend to
define in the program.
Function 3
A function is a self-contained block of statements
that performs a particular specified task.

Function with
argument &
Library return values
Function Function with
Type of
Function argument & no
User-defined return values
Functions Function with
no argument &
return values
Function with
no argument &
no return values
Control Flow in 4

a multi-function
program
Advantages of Function 5

Manageability
Code Reusability
Non-redundant Programming
Logical Clarity
Easy to divide the work among programmers
Library Functions 6
(Built-in Functions)
Library functions in C language are inbuilt functions
which are grouped together and placed in a
common place called library.

Header File Description


stdio.h For Standard input/output
conio.h For Console input/output
string.h All string related functions are defined
stdlib.h Contains general functions
math.h Contains all math related functions
time.h Contains time and clock related functions
ctype.h For Character handling functions
User-defined Functions 7

Types of User-defined Functions


1. Function with argument & return values
2. Function with argument & no return values
3. Function with no argument & return values
4. Function with no argument & no return values
Components of Function 8

Function Definition
Function Declarator & Function Body
Function Declaration or Prototype
Return Statement
Accessing/Calling a function
Function Parameters (Arguments)
Function Definition 9

int find_max(int n1, int n2) {


int max;
if (n1 >n2){
max =n1;
}else{
max =n2;
}
return max;
}
Return Statement 10

int find_max(int n1, int n2) {


int max;
if (n1 >n2){
max =n1;
}else{
max =n2;
}
return max;
}
Function Prototype 11
Function Call 12
Category of User-defined Functions 13
according to Return Values & Arguments

1. Function with argument & return values


2. Function with argument & no return values
3. Function with no argument & return values
4. Function with no argument & no return values
1. Function with argument 14

& return values


2. Function with argument 15

& no return values


3. Function with no 16

argument & return values


4. Function with no argument 17
& no return values
Task 18

Write four separate programs to add two


entered integers using all type of user-
defined functions.
Expected Output:
Enter a: 5
Enter b: 7
Sum =12
Types of Function Call 19

1. Function call by value (or Pass arguments by


value)
2. Function call by reference(or Pass arguments
by address)
Call By Value 20
Call By Reference 21
Recursive Functions 22
Macros 23
Macros with Argument 24
Scope, Visibility & Lifetime 25

of Variables
1. Automatic Variables,
2. External Variables,
3. Static Variables,
4. Register Variables
Automatic Variables 26

Automatic variable is a local variable which is


allocated and de-allocated automatically when
program flow enters and leaves the variable's
scope. The keyword auto is used to declare
automatic variables explicitly.
main(){
auto int number;

}
External Variables 27

It is possible to define variables that are external


to all functions, that is, variables that can be
accessed by name by any function. External
variables are both active and alive throughout
the entire program so these are also known as
global variables.
int global_variable;
void main(void) {
global_variable =1;
// Statements
}
Static Variables 28
Static variables have a
property of preserving their
value even after they are out
of their scope. Hence, static
variables preserve their
previous value in their previous
scope and are not initialized
again in the new scope.
Syntax:
static data_type var_name =
var_value;
E.g. static int a =5;
Register Variables

Registers are faster than memory to access, so the variables which


are most frequently used in a C program can be put in registers
using register keyword. Register variables, are a special case of
automatic variables, are kept by Compiler in one of the machine’s
registers instead of keeping in the memory for faster execution.
Syntax:
register data_type variable_name =value;
E.g. register int count =0;

You might also like