SlideShare a Scribd company logo
Function
Introduction
A complex problem is often easier to solve by dividing it into
several smaller parts, each of which can be solved by itself.
This is called structured programming.
These parts are made into functions in C.These parts are made into functions in C.
main() then uses these functions to solve the original
problem.
2 Dept. of CSE&IT, DBCET, Guwahati
Definition
A function is a named, independent section of C code that
performs a specific task and optionally returns a value to the
calling program or/and receives values(s) from the calling
program.
3 Dept. of CSE&IT, DBCET, Guwahati
Categories of Function
Basically there are two categories of function:
1. Predefined functions: available in C / C++ standard library
such as stdio.h, math.h, string.h etc.
2. User-defined functions: functions that programmers create
for specialized tasks such as graphic and multimediafor specialized tasks such as graphic and multimedia
libraries, implementation extensions or dependent etc.
4 Dept. of CSE&IT, DBCET, Guwahati
Function Characteristics
Basically a function has the following characteristics:
1. Named with unique name .
2. Performs a specific task -Task is a discrete job that the program
must perform as part of its overall operation, such as sending a line
of text to the printer, sorting an array into numerical order, or
calculating a cube root, etc.calculating a cube root, etc.
3. Independent - A function can perform its task without
interference from or interfering with other parts of the program.
4. May receive values from the calling program (caller) -
Calling program can pass values to function for processing whether
directly or indirectly (by reference).
5. May return a value to the calling program – the called
function may pass something back to the calling program.
5 Dept. of CSE&IT, DBCET, Guwahati
Advantages of Function
Functions separates the concept (what is done)
from the implementation (how it is done).
Functions make programs easier to understand.
Functions can be called several times in the sameFunctions can be called several times in the same
program, allowing the code to be reused.
6 Dept. of CSE&IT, DBCET, Guwahati
Function Prototype
Function prototype consists of
Function name
Parameters – what the function takes in
Return type – data type function returns (default int)
Used to validate functionsUsed to validate functions
The function with the prototype
int maximum( int, int, int );
Takes in 3 ints
Returns an int
7 Dept. of CSE&IT, DBCET, Guwahati
Function Definition
Function definition format
return-value-type function-name( parameter-list )
{
declarations and statements
}
Function-name: any valid identifierFunction-name: any valid identifier
Return-value-type: data type of the result (default int)
void – indicates that the function returns nothing
Parameter-list: comma separated list, declares parameters
A type must be listed explicitly for each parameter unless, the parameter
is of type int
8 Dept. of CSE&IT, DBCET, Guwahati
Function Definition [contd..]
Declarations and statements: function body (block)
Variables can be declared inside blocks (can be nested)
Functions can not be defined inside other functions
Returning control
If nothing returnedIf nothing returned
return;
or, until reaches right brace
If something returned
return expression;
9 Dept. of CSE&IT, DBCET, Guwahati
Function Call
Invoking functions
Provide function name and arguments (data)
Function performs operations or manipulations
Function returns results
Format for calling functions
FunctionName( argument );
If multiple arguments, use comma-separated list
printf( "%.2f", sqrt( 900.0 ) );
Calls function sqrt, which returns the square root of its argument
All math functions return data type double
Arguments may be constants, variables, or expressions
10 Dept. of CSE&IT, DBCET, Guwahati
Example
#include <stdio.h>
int absolute(int); // function prototype for absolute()
int main(){
int num, answer;
printf("Enter an integer: “);
scaf(“%d”,&num);
answer = absolute(num); //function call
printf("The absolute value of is %d“, answer);
return 0; }
// Define a function to take absolute value of an integer
int absolute(int x) // function definition
{
if (x >= 0)
return x;
else
return -x;
}
11 Dept. of CSE&IT, DBCET, Guwahati
Purpose of return statement
The return statement serves two purposes:
On executing the return statement it immediately transfers
the control back to the calling program.
It returns the value present in the parentheses after return, to
the calling program.the calling program.
12 Dept. of CSE&IT, DBCET, Guwahati
Function without arguments
and without return value
#include<stdio.h>
void addnumbers(void);
void main()
{
printf(“ Program for adding two numbers using functions n”);
addnumbers();
printf(“n we are back in main function”);printf(“n we are back in main function”);
}
void addnumbers()
{
int a,b,sum;
printf(“enter the values of two numbers:”);
scanf(“%d%d”,&a,&b);
sum=a + b;
printf(“The sum of %d and %d is = %d”,a,b,sum);
}
13 Dept. of CSE&IT, DBCET, Guwahati
One function can also calls another function i.e., function
calls can be nested.
A function can also calls itself. These types of functions
are called recursion.
void main()
{ int fact(int a){
calculate();
}
void calculate()
{
add();
mul();
}
void add()
{
}
void mul()
{
}
int fact(int a)
{
f=a*fact(a-1);
}
14 Dept. of CSE&IT, DBCET, Guwahati
Library functions
Each standard library has a corresponding header file
Header files contain function prototypes and other
definitions
Can also create library of your own functions
There’s a list of available functions in many Libraries
stdio.h, math.h, string.h, conio.h etc…stdio.h, math.h, string.h, conio.h etc…
clrscr() in conio.h
Clears text mode window
15 Dept. of CSE&IT, DBCET, Guwahati
abs acos acosl asin asinl
atan atanl atan2 atan2l atof _atold
cabs cabsl ceil ceill cos cosl
cosh coshl exp expl fabs fabsl
floor floorl fmod fmodl frexp frexpl
Math.h
hypot hypotl labs ldexp ldexpl
log logl log10 log101 matherr _matherrl
modf modfl poly polyl pow powl
pow10 pow10l sin sinl sinh sinhl
sqrt sqrtl tan tanl tanh tanhl
16 Dept. of CSE&IT, DBCET, Guwahati
stdio.h
clearerr fclose fcloseall fdopen feof ferror
fflush fgetc fgetchar fgetpos fgets fileno
flushall fopen fprintf fputc fputchar fputs
fread freopen fscanf fseek fsetpos ftell
fwrite getc getchar gets getw perror
printf putc putchar puts putw removeprintf putc putchar puts putw remove
rename rewind rmtmp scanf setbuf setvbuf
sprintf sscanf strerror _strerror tempnam tmpfile
tmpnam ungetc unlink vfprintf vfscanf vprintf
vscanf vsprintf vsscanf
17 Dept. of CSE&IT, DBCET, Guwahati
Different ways of calling a function
with arguments
When calling a function, arguments can be passed to a
function in two ways
Call by value
Call by reference
18 Dept. of CSE&IT, DBCET, Guwahati
Call By ValueCall By ValueCall By ValueCall By Value
Here arguments are being passed by value
temporary copy of argument (constant, variable, expression)
is provided to function (by way of a stack)
function can change the copy, but not the original value in
calling function
Call by value results in greater independence betweenCall by value results in greater independence between
modules
19 Dept. of CSE&IT, DBCET, Guwahati
20 Dept. of CSE&IT, DBCET, Guwahati
#include<stdio.h>
void callbyval(int,int);
void main()
{
int a,b;
a=b=10;
printf(“nThe values of a and b before calling the function is %d and %d”,a,b);
Example
callbyval(a,b);
printf(“nAtfer the function is executed the values of a is %d and b is %d”,a,b);
}
void callbyval(int a,int b)
{
a=a * a;
b=b * b;
printf(“nThe values of a and b inside the callbyval function is %d and %d”,a,b);
}
21 Dept. of CSE&IT, DBCET, Guwahati
Scope Rules
Scope of a variable is that part of a program in which the variable can be
referenced
visible only in the block in which they are declared
void display(int); 1. Variable accessible in main() is ivoid display(int);
void main()
{ int i=10;
display(i);
}
void display(int j)
{ int k=27;
printf(“n %d %d”,j,k);
}
1. Variable accessible in main() is i
2. Variables accessible in display is j and
k
3. Scope of i is within the main() and
scope of j and k is in the display()
22 Dept. of CSE&IT, DBCET, Guwahati
An Introduction to pointersAn Introduction to pointersAn Introduction to pointersAn Introduction to pointers
2
a
4885
Location name
Value at location
Location address
This declaration tell the C compiler to:
1. Reserve space in memory to hold an integer
value
2. Associate the name “a” with this memory
location
3. Store the value 2 at this location
int a=2;
A pointer is a variable that contains the address of a variable
Pointers are declared to "point" to a variable of a particular type
(int, double, etc.)
When we use a pointer to access the value stored in the variable
to which it points, we are using indirect addressing
23 Dept. of CSE&IT, DBCET, Guwahati
2
a
4885
4885
b
3376
int a=2;
int *b;
Is a pointer to an
integer
int *b;
b=&a;
printf(“nAddress of a=%u”,&a);
printf(“nAddress of a=%u”,b);
printf(“nAddress of b=%u”,&b);
printf(“nValue of b=%u”,b);
printf(“nValue of a=%d”,a);
printf(“nValue of a=%d”,*(&a));
printf(“nValue of a=%d”,*b);
Address of a=4885
Address of a=4885
Address of b=3376
Value of b=4885
Value of a=2
Value of a=2
Value of a=2
24 Dept. of CSE&IT, DBCET, Guwahati
Call By ReferenceCall By ReferenceCall By ReferenceCall By Reference
passed by reference
temporary copy of the address of argument is provided to function
called function can change value of the local variable in the calling
function because it knows where in memory it isfunction because it knows where in memory it is
pass by reference simulated in C by using the address operator
(&), an array name, or a pointer variable
25 Dept. of CSE&IT, DBCET, Guwahati
void swap(int *x,int *y);
void main()
{
int a=10,b=20;
swap(&a,&b);
printf(“n a=%d b=%d”,a,b);
20
b
8682
10
a
4885
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
10
t
2416
4885
x
6612
8682
y
4216
10
b
8682
20
a
4885
26 Dept. of CSE&IT, DBCET, Guwahati
Recursion
In C, a function that calls itself repeatedly, is called a recursion.
Using recursion sometimes makes coding more straightforward;
consider the calculation of n!,
27 Dept. of CSE&IT, DBCET, Guwahati
Recursion Example
#include<stdio.h>
long int fact( unsigned int num);
void main();
{ long f;
unsigned int n;
printf(“Enter an integer number:”);
scanf(“%u”,&n);
f=fact(n);
printf(“The factorial of a number is %ld n”,f);printf(“The factorial of a number is %ld n”,f);
}
long int fact(unsigned int num)
{
if(num==0)
return(1);
else
return ( num * fact(num-1));
}
28 Dept. of CSE&IT, DBCET, Guwahati
f=fact(4);
1st call to fact
num=4;
return(4 * fact(3));
2nd call to fact
num =3;
return(3 * fact(2));
2
6
24
long int fact(unsigned int num)
{
if(num==0)
return(1);
else
return ( num * fact(num-1)); return(3 * fact(2));
3rd call to fact
num =2;
return(2 * fact(1));
4th call to fact
num =1;
return(1 * fact(0));
5th call to fact
num =0;
return(1)
1
1
return ( num * fact(num-1));
}
29 Dept. of CSE&IT, DBCET, Guwahati

More Related Content

What's hot

Preprocessor
PreprocessorPreprocessor
Preprocessor
Võ Hòa
 
C programming session7
C programming  session7C programming  session7
C programming session7
Keroles karam khalil
 
Notes part5
Notes part5Notes part5
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
TejaswiB4
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
tanmaymodi4
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
adarshynl
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
Vineet Kumar Saini
 
Functions in C
Functions in CFunctions in C
Functions in C
Kamal Acharya
 
Lesson 7 io statements
Lesson 7 io statementsLesson 7 io statements
Lesson 7 io statements
Dr. Rupinder Singh
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
Mansi Tyagi
 
Functions in c
Functions in cFunctions in c
Functions in c
reshmy12
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Harsh Pathak
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
hasan Mohammad
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
P3 InfoTech Solutions Pvt. Ltd.
 
structured programming
structured programmingstructured programming
structured programming
Ahmad54321
 
C++ questions and answers
C++ questions and answersC++ questions and answers
C++ questions and answers
Deepak Singh
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
imtiazalijoono
 

What's hot (20)

Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
Preprocessor
PreprocessorPreprocessor
Preprocessor
 
C programming session7
C programming  session7C programming  session7
C programming session7
 
Notes part5
Notes part5Notes part5
Notes part5
 
Basic structure of c programming
Basic structure of c programmingBasic structure of c programming
Basic structure of c programming
 
ANSI C Macros
ANSI C MacrosANSI C Macros
ANSI C Macros
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
C programming interview questions
C programming interview questionsC programming interview questions
C programming interview questions
 
Top C Language Interview Questions and Answer
Top C Language Interview Questions and AnswerTop C Language Interview Questions and Answer
Top C Language Interview Questions and Answer
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Lesson 7 io statements
Lesson 7 io statementsLesson 7 io statements
Lesson 7 io statements
 
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM) FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
FUNCTION IN C PROGRAMMING UNIT -6 (BCA I SEM)
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
6 preprocessor macro header
6 preprocessor macro header6 preprocessor macro header
6 preprocessor macro header
 
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
Python Programming Essentials - M19 - Namespaces, Global Variables and Docstr...
 
structured programming
structured programmingstructured programming
structured programming
 
C++ questions and answers
C++ questions and answersC++ questions and answers
C++ questions and answers
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 

Similar to Preprocessor directives

Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
Mehul Desai
 
Functions part1
Functions part1Functions part1
Functions part1
yndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
yndaravind
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
Syed Mustafa
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
MehakBhatia38
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
C function
C functionC function
C function
thirumalaikumar3
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde3
 
Function in c program
Function in c programFunction in c program
Function in c program
umesh patil
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
Umesh Nikam
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
BoomBoomers
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
vekariyakashyap
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 

Similar to Preprocessor directives (20)

Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Functions part1
Functions part1Functions part1
Functions part1
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Pointers and call by value, reference, address in C
Pointers and call by value, reference, address in CPointers and call by value, reference, address in C
Pointers and call by value, reference, address in C
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
functions in c language_functions in c language.pptx
functions in c language_functions in c language.pptxfunctions in c language_functions in c language.pptx
functions in c language_functions in c language.pptx
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
C function
C functionC function
C function
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Object Oriented Technologies
Object Oriented TechnologiesObject Oriented Technologies
Object Oriented Technologies
 
USER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdfUSER DEFINED FUNCTIONS IN C.pdf
USER DEFINED FUNCTIONS IN C.pdf
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 

More from Vikash Dhal

Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
Vikash Dhal
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
Vikash Dhal
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassensVikash Dhal
 
Packet switching
Packet switchingPacket switching
Packet switchingVikash Dhal
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++Vikash Dhal
 
File handling in c
File handling in c File handling in c
File handling in c
Vikash Dhal
 

More from Vikash Dhal (7)

Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
C fundamentals
C fundamentalsC fundamentals
C fundamentals
 
Implementation of strassens
Implementation of  strassensImplementation of  strassens
Implementation of strassens
 
Packet switching
Packet switchingPacket switching
Packet switching
 
Raid
RaidRaid
Raid
 
Strinng Classes in c++
Strinng Classes in c++Strinng Classes in c++
Strinng Classes in c++
 
File handling in c
File handling in c File handling in c
File handling in c
 

Recently uploaded

power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
Kamal Acharya
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
Jayaprasanna4
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
Kamal Acharya
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
AJAYKUMARPUND1
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 

Recently uploaded (20)

power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Event Management System Vb Net Project Report.pdf
Event Management System Vb Net  Project Report.pdfEvent Management System Vb Net  Project Report.pdf
Event Management System Vb Net Project Report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
ethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.pptethical hacking in wireless-hacking1.ppt
ethical hacking in wireless-hacking1.ppt
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
Automobile Management System Project Report.pdf
Automobile Management System Project Report.pdfAutomobile Management System Project Report.pdf
Automobile Management System Project Report.pdf
 
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
Pile Foundation by Venkatesh Taduvai (Sub Geotechnical Engineering II)-conver...
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 

Preprocessor directives

  • 2. Introduction A complex problem is often easier to solve by dividing it into several smaller parts, each of which can be solved by itself. This is called structured programming. These parts are made into functions in C.These parts are made into functions in C. main() then uses these functions to solve the original problem. 2 Dept. of CSE&IT, DBCET, Guwahati
  • 3. Definition A function is a named, independent section of C code that performs a specific task and optionally returns a value to the calling program or/and receives values(s) from the calling program. 3 Dept. of CSE&IT, DBCET, Guwahati
  • 4. Categories of Function Basically there are two categories of function: 1. Predefined functions: available in C / C++ standard library such as stdio.h, math.h, string.h etc. 2. User-defined functions: functions that programmers create for specialized tasks such as graphic and multimediafor specialized tasks such as graphic and multimedia libraries, implementation extensions or dependent etc. 4 Dept. of CSE&IT, DBCET, Guwahati
  • 5. Function Characteristics Basically a function has the following characteristics: 1. Named with unique name . 2. Performs a specific task -Task is a discrete job that the program must perform as part of its overall operation, such as sending a line of text to the printer, sorting an array into numerical order, or calculating a cube root, etc.calculating a cube root, etc. 3. Independent - A function can perform its task without interference from or interfering with other parts of the program. 4. May receive values from the calling program (caller) - Calling program can pass values to function for processing whether directly or indirectly (by reference). 5. May return a value to the calling program – the called function may pass something back to the calling program. 5 Dept. of CSE&IT, DBCET, Guwahati
  • 6. Advantages of Function Functions separates the concept (what is done) from the implementation (how it is done). Functions make programs easier to understand. Functions can be called several times in the sameFunctions can be called several times in the same program, allowing the code to be reused. 6 Dept. of CSE&IT, DBCET, Guwahati
  • 7. Function Prototype Function prototype consists of Function name Parameters – what the function takes in Return type – data type function returns (default int) Used to validate functionsUsed to validate functions The function with the prototype int maximum( int, int, int ); Takes in 3 ints Returns an int 7 Dept. of CSE&IT, DBCET, Guwahati
  • 8. Function Definition Function definition format return-value-type function-name( parameter-list ) { declarations and statements } Function-name: any valid identifierFunction-name: any valid identifier Return-value-type: data type of the result (default int) void – indicates that the function returns nothing Parameter-list: comma separated list, declares parameters A type must be listed explicitly for each parameter unless, the parameter is of type int 8 Dept. of CSE&IT, DBCET, Guwahati
  • 9. Function Definition [contd..] Declarations and statements: function body (block) Variables can be declared inside blocks (can be nested) Functions can not be defined inside other functions Returning control If nothing returnedIf nothing returned return; or, until reaches right brace If something returned return expression; 9 Dept. of CSE&IT, DBCET, Guwahati
  • 10. Function Call Invoking functions Provide function name and arguments (data) Function performs operations or manipulations Function returns results Format for calling functions FunctionName( argument ); If multiple arguments, use comma-separated list printf( "%.2f", sqrt( 900.0 ) ); Calls function sqrt, which returns the square root of its argument All math functions return data type double Arguments may be constants, variables, or expressions 10 Dept. of CSE&IT, DBCET, Guwahati
  • 11. Example #include <stdio.h> int absolute(int); // function prototype for absolute() int main(){ int num, answer; printf("Enter an integer: “); scaf(“%d”,&num); answer = absolute(num); //function call printf("The absolute value of is %d“, answer); return 0; } // Define a function to take absolute value of an integer int absolute(int x) // function definition { if (x >= 0) return x; else return -x; } 11 Dept. of CSE&IT, DBCET, Guwahati
  • 12. Purpose of return statement The return statement serves two purposes: On executing the return statement it immediately transfers the control back to the calling program. It returns the value present in the parentheses after return, to the calling program.the calling program. 12 Dept. of CSE&IT, DBCET, Guwahati
  • 13. Function without arguments and without return value #include<stdio.h> void addnumbers(void); void main() { printf(“ Program for adding two numbers using functions n”); addnumbers(); printf(“n we are back in main function”);printf(“n we are back in main function”); } void addnumbers() { int a,b,sum; printf(“enter the values of two numbers:”); scanf(“%d%d”,&a,&b); sum=a + b; printf(“The sum of %d and %d is = %d”,a,b,sum); } 13 Dept. of CSE&IT, DBCET, Guwahati
  • 14. One function can also calls another function i.e., function calls can be nested. A function can also calls itself. These types of functions are called recursion. void main() { int fact(int a){ calculate(); } void calculate() { add(); mul(); } void add() { } void mul() { } int fact(int a) { f=a*fact(a-1); } 14 Dept. of CSE&IT, DBCET, Guwahati
  • 15. Library functions Each standard library has a corresponding header file Header files contain function prototypes and other definitions Can also create library of your own functions There’s a list of available functions in many Libraries stdio.h, math.h, string.h, conio.h etc…stdio.h, math.h, string.h, conio.h etc… clrscr() in conio.h Clears text mode window 15 Dept. of CSE&IT, DBCET, Guwahati
  • 16. abs acos acosl asin asinl atan atanl atan2 atan2l atof _atold cabs cabsl ceil ceill cos cosl cosh coshl exp expl fabs fabsl floor floorl fmod fmodl frexp frexpl Math.h hypot hypotl labs ldexp ldexpl log logl log10 log101 matherr _matherrl modf modfl poly polyl pow powl pow10 pow10l sin sinl sinh sinhl sqrt sqrtl tan tanl tanh tanhl 16 Dept. of CSE&IT, DBCET, Guwahati
  • 17. stdio.h clearerr fclose fcloseall fdopen feof ferror fflush fgetc fgetchar fgetpos fgets fileno flushall fopen fprintf fputc fputchar fputs fread freopen fscanf fseek fsetpos ftell fwrite getc getchar gets getw perror printf putc putchar puts putw removeprintf putc putchar puts putw remove rename rewind rmtmp scanf setbuf setvbuf sprintf sscanf strerror _strerror tempnam tmpfile tmpnam ungetc unlink vfprintf vfscanf vprintf vscanf vsprintf vsscanf 17 Dept. of CSE&IT, DBCET, Guwahati
  • 18. Different ways of calling a function with arguments When calling a function, arguments can be passed to a function in two ways Call by value Call by reference 18 Dept. of CSE&IT, DBCET, Guwahati
  • 19. Call By ValueCall By ValueCall By ValueCall By Value Here arguments are being passed by value temporary copy of argument (constant, variable, expression) is provided to function (by way of a stack) function can change the copy, but not the original value in calling function Call by value results in greater independence betweenCall by value results in greater independence between modules 19 Dept. of CSE&IT, DBCET, Guwahati
  • 20. 20 Dept. of CSE&IT, DBCET, Guwahati
  • 21. #include<stdio.h> void callbyval(int,int); void main() { int a,b; a=b=10; printf(“nThe values of a and b before calling the function is %d and %d”,a,b); Example callbyval(a,b); printf(“nAtfer the function is executed the values of a is %d and b is %d”,a,b); } void callbyval(int a,int b) { a=a * a; b=b * b; printf(“nThe values of a and b inside the callbyval function is %d and %d”,a,b); } 21 Dept. of CSE&IT, DBCET, Guwahati
  • 22. Scope Rules Scope of a variable is that part of a program in which the variable can be referenced visible only in the block in which they are declared void display(int); 1. Variable accessible in main() is ivoid display(int); void main() { int i=10; display(i); } void display(int j) { int k=27; printf(“n %d %d”,j,k); } 1. Variable accessible in main() is i 2. Variables accessible in display is j and k 3. Scope of i is within the main() and scope of j and k is in the display() 22 Dept. of CSE&IT, DBCET, Guwahati
  • 23. An Introduction to pointersAn Introduction to pointersAn Introduction to pointersAn Introduction to pointers 2 a 4885 Location name Value at location Location address This declaration tell the C compiler to: 1. Reserve space in memory to hold an integer value 2. Associate the name “a” with this memory location 3. Store the value 2 at this location int a=2; A pointer is a variable that contains the address of a variable Pointers are declared to "point" to a variable of a particular type (int, double, etc.) When we use a pointer to access the value stored in the variable to which it points, we are using indirect addressing 23 Dept. of CSE&IT, DBCET, Guwahati
  • 24. 2 a 4885 4885 b 3376 int a=2; int *b; Is a pointer to an integer int *b; b=&a; printf(“nAddress of a=%u”,&a); printf(“nAddress of a=%u”,b); printf(“nAddress of b=%u”,&b); printf(“nValue of b=%u”,b); printf(“nValue of a=%d”,a); printf(“nValue of a=%d”,*(&a)); printf(“nValue of a=%d”,*b); Address of a=4885 Address of a=4885 Address of b=3376 Value of b=4885 Value of a=2 Value of a=2 Value of a=2 24 Dept. of CSE&IT, DBCET, Guwahati
  • 25. Call By ReferenceCall By ReferenceCall By ReferenceCall By Reference passed by reference temporary copy of the address of argument is provided to function called function can change value of the local variable in the calling function because it knows where in memory it isfunction because it knows where in memory it is pass by reference simulated in C by using the address operator (&), an array name, or a pointer variable 25 Dept. of CSE&IT, DBCET, Guwahati
  • 26. void swap(int *x,int *y); void main() { int a=10,b=20; swap(&a,&b); printf(“n a=%d b=%d”,a,b); 20 b 8682 10 a 4885 } void swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; } 10 t 2416 4885 x 6612 8682 y 4216 10 b 8682 20 a 4885 26 Dept. of CSE&IT, DBCET, Guwahati
  • 27. Recursion In C, a function that calls itself repeatedly, is called a recursion. Using recursion sometimes makes coding more straightforward; consider the calculation of n!, 27 Dept. of CSE&IT, DBCET, Guwahati
  • 28. Recursion Example #include<stdio.h> long int fact( unsigned int num); void main(); { long f; unsigned int n; printf(“Enter an integer number:”); scanf(“%u”,&n); f=fact(n); printf(“The factorial of a number is %ld n”,f);printf(“The factorial of a number is %ld n”,f); } long int fact(unsigned int num) { if(num==0) return(1); else return ( num * fact(num-1)); } 28 Dept. of CSE&IT, DBCET, Guwahati
  • 29. f=fact(4); 1st call to fact num=4; return(4 * fact(3)); 2nd call to fact num =3; return(3 * fact(2)); 2 6 24 long int fact(unsigned int num) { if(num==0) return(1); else return ( num * fact(num-1)); return(3 * fact(2)); 3rd call to fact num =2; return(2 * fact(1)); 4th call to fact num =1; return(1 * fact(0)); 5th call to fact num =0; return(1) 1 1 return ( num * fact(num-1)); } 29 Dept. of CSE&IT, DBCET, Guwahati