SlideShare une entreprise Scribd logo
1  sur  28
Sigma institute of engineering
SUBJECT: COMPUTER PROGRAMMING
AND UTILIZATION
PREPARED BY : group c
 ENROLLMENT NO:- NAME OF STUDENT
1. 140500119041 KANADE MAHENDRA
2. 140500119052 MAKWANA BHAVESH
3. 140500119054 MEHUL JHALA
ASSIGNED BY ;
PROF.SURESH CHAUDHARY
Topic :
functions
CONTENTS:
 Function
 Function example
 Vedio on functions
 Benefits of function
 Math library functions function prototype
 Function arguments
 C++ variables
 Global variable
 External variables
 Other variables
 Recursion
 Recursion example
Functions
 A function is a self contained block of code
that performs a particular task.
 Any C program can be seen as a
collection/group of these functions.
 A functions takes some data as input, perform
some operation on that data and then return
a value.
 Any C program must contain at least one
function, which is main().
 There is no limit on the number of functions
that might be present in a C program.
 For ex.
main()
{
message();
printf(“I am in main”);
}
message()
{
printf(“I am in message”);
}
Video on function
Benefits of functions
Divide and conquer
Manageable program development
Software reusability
Use existing functions as building
blocks for new programs
Abstraction - hide internal details
(library functions)
Avoid code repetition
5.3Math Library Functions
 Math library functions
 perform common mathematical calculations
 #include <math.h>
 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
Method Description Example
ceil( x ) rounds x to the smallest integer
not less than x
ceil( 9.2 ) is 10.0
ceil( -9.8 ) is -9.0
cos( x ) trigonometric cosine of x
(x in radians)
cos( 0.0 ) is 1.0
exp( x ) exponential function ex exp( 1.0 ) is 2.71828
exp( 2.0 ) is 7.38906
fabs( x ) absolute value of x fabs( 5.1 ) is 5.1
fabs( 0.0 ) is 0.0
fabs( -8.76 ) is 8.76
floor( x ) rounds x to the largest integer
not greater than x
floor( 9.2 ) is 9.0
floor( -9.8 ) is -10.0
fmod( x, y ) remainder of x/y as a floating-
point number
fmod( 13.657, 2.333 ) is 1.992
log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0
log( 7.389056 ) is 2.0
log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0
log10( 100.0 ) is 2.0
pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128
pow( 9, .5 ) is 3
sin( x ) trigonometric sine of x
(x in radians)
sin( 0.0 ) is 0
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0
tan( x ) trigonometric tangent of x
(x in radians)
tan( 0.0 ) is 0
Fig. 3.2 Math library functions.
Math Library Functions Revisited
5.2Program Modules in C Functions
 Modules in C
 Programs combine user-defined functions with library
functions
 C standard library has a wide variety of functions
 Function calls
 Invoking functions
 Provide function name and arguments (data)
 Function performs operations or manipulations
 Function returns results
 Function call analogy:
 Boss asks worker to complete task
 Worker gets information, does task, returns
result
 Information hiding: boss does not know details
Function Prototype
 All Identifiers in C must be declared before they are used. This is
true for functions as well as variables.
 For functions, the declarations needs to be done before the first
call of the function.
 A function declaration specifies the name, return type, and
arguments of a function. This is also called the function
prototype.
 To be a prototype, a function declaration must establish types for
the function’s arguments.
 Having the prototype available before the first use of the function
allows the compiler to check that the correct number and types of
arguments are used in the function call.
 The prototype has the same syntax as the
function definition, except that it is
terminated by a semicolon following the
closing parenthesis and therefore has no
body.
 Although, functions that return int values
do not require prototypes, prototypes are
recommended.
Function Definition
 General form of any function definition is:
return-type function-name(argument declarations)
{
declarations and statements
}
 Return-type refers to the data type of the value
being returned from the function. If the return
type is omitted, int is assumed.
 The values provided to a function for processing
are the arguments.
 The set of statements between the braces is
called as the function body.
Arguments – Call by Value
 In C, all functions are passed “by value” by default.
 This means that the called function is given the values of its
arguments in temporary variables rather than the originals.
 For ex. main()
{
int a=4,b=5;
sum(a,b);
printf(“Sum = %d”,a+b);
}
sum(int a,int b)
{
a++;
b++;
printf(“Sum = %d”,a+b);
}
Arguments – Call by Reference
 When necessary, it is possible to arrange a function which can modify a variable
in a calling routine.
 The caller must provide the address of the variable to be set (generally, a
pointer to a variable), and the called function must declare the parameter to be
a pointer and access the variable indirectly through it.
 For ex: main()
{
int a=4,b=5;
sum(&a,&b);
printf(“Sum = %d”,a+b);
}
sum(int *a,int *b)
{
(*a)++;
(*b)++;
printf(“Sum = %d”,(*a)+(*b));
}
C++ Variables
• A variable is a place in memory that has
– A name or identifier (e.g. income, taxes, etc.)
– A data type (e.g. int, double, char, etc.)
– A size (number of bytes)
– A scope (the part of the program code that can use it)
• Global variables – all functions can see it and using it
• Local variables – only the function that declare local variables
see and use these variables
– A life time (the duration of its existence)
• Global variables can live as long as the program is executed
• Local variables are lived only when the functions that define
these variables are executed
16
Local Variables
• Local variables are declared inside the function
body and exist as long as the function is running
and destroyed when the function exit
• You have to initialize the local variable before
using it
• If a function defines a local variable and there was
a global variable with the same name, the function
uses its local variable instead of using the global
variable
17
Example of Defining and Using Global
and Local Variables
#include <iostream.h>
int x; // Global variable
Void fun(); // function signature
void main()
{
x = 4;
fun();
cout << x << endl;
}
void fun()
{
int x = 10; // Local variable
cout << x << endl;
}
18
Local vs Global Variables
#include<iostream.h>
int x,y; //Global Variables
int add2(int, int); //prototype
main()
{ int s;
x = 11;
y = 22;
cout << “global x=” << x << endl;
cout << “Global y=” << y << endl;
s = add2(x, y);
cout << x << “+” << y << “=“ << s;
cout<<endl;
cout<<“n---end of output---n”;
return 0;
}
int add2(int x1,int y1)
{ int x; //local variables
x=44;
cout << “nLocal x=” << x << endl;
return x1+y1;
}
global x=11
global y=22
Local x=44
11+22=33
---end of output---
External Variables
 See the below example:
main()
{
extern int a;
printf(“Value of a = %d”,a);
}
int a=5;
Output: Value of a = 5
Recursion
 Recursion defines a function in terms of itself.
 It is a programming technique in which a
function calls itself.
 A recursive function calls itself repeatedly,
with different argument values each time.
 Some argument values cause the recursive
method to return without calling itself. This is
the base case.
 Either omitting the base case or writing the
recursion step incorrectly will cause infinite
recursion (stack overflow error).
 Recursion to find the factorial of any number:
int factorial(int x)
{
if(x<=1)
return 1;
else
return(x*factorial(x-1));
}
 Example: factorials
 5! = 5 * 4 * 3 * 2 * 1
 Notice that
 5! = 5 * 4!
 4! = 4 * 3! ...
 Can compute factorials recursively
 Solve base case (1! = 0! = 1) then plug in
 2! = 2 * 1! = 2 * 1 = 2;
 3! = 3 * 2! = 3 * 2 = 6;
5.14 Example Using
Recursion: The Fibonacci Series
 Fibonacci series: 0, 1, 1, 2, 3, 5, 8...
 Each number is the sum of the previous two
 Can be solved recursively:
 fib( n ) = fib( n - 1 ) + fib( n – 2 )
 Code for the fibaonacci function
long fibonacci( long n )
{
if (n == 0 || n == 1) // base case
return n;
else
return fibonacci( n - 1) +
fibonacci( n – 2 );
}
5.14 Example Using
Recursion: The Fibonacci Series
Set of recursive calls to function fibonaccif( 3 )
f( 1 )f( 2 )
f( 1 ) f( 0 ) return 1
return 1 return 0
return +
+return
1 /* Fig. 5.15: fig05_15.c
2 Recursive fibonacci function */
3 #include <stdio.h>
4
5 long fibonacci( long );
6
7 int main()
8 {
9 long result, number;
10
11 printf( "Enter an integer: " );
12 scanf( "%ld", &number );
13 result = fibonacci( number );
14 printf( "Fibonacci( %ld ) = %ldn", number, result );
15 return 0;
16 }
17
18 /* Recursive definition of function fibonacci */
19 long fibonacci( long n )
20 {
21 if ( n == 0 || n == 1 )
22 return n;
23 else
24 return fibonacci( n - 1 ) + fibonacci( n - 2 );
25 }
output:
Enter an integer: 0
Fibonacci(0) = 0
Enter an integer: 1
Fibonacci(1) = 1
 Program
Output
Enter an integer: 2
Fibonacci(2) = 1
Enter an integer: 3
Fibonacci(3) = 2
Enter an integer: 4
Fibonacci(4) = 3
Enter an integer: 5
Fibonacci(5) = 5
Enter an integer: 6
Fibonacci(6) = 8
Enter an integer: 10
Fibonacci(10) = 55
Enter an integer: 20
Fibonacci(20) = 6765
Enter an integer: 30
Fibonacci(30) = 832040
Enter an integer: 35
Fibonacci(35) = 9227465
Thank You..

Contenu connexe

Tendances (20)

Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Function in c
Function in cFunction in c
Function in c
 
Advanced C - Part 3
Advanced C - Part 3Advanced C - Part 3
Advanced C - Part 3
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
C Prog - Functions
C Prog - FunctionsC Prog - Functions
C Prog - Functions
 
Fp201 unit5 1
Fp201 unit5 1Fp201 unit5 1
Fp201 unit5 1
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
Function in c
Function in cFunction in c
Function in c
 
Functions
FunctionsFunctions
Functions
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
Function in c
Function in cFunction in c
Function in c
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
03 function overloading
03 function overloading03 function overloading
03 function overloading
 
C++ programming function
C++ programming functionC++ programming function
C++ programming function
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Function in c program
Function in c programFunction in c program
Function in c program
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 

En vedette

Biases in irish health service statistics w behan 2014
Biases in irish health service statistics w behan 2014Biases in irish health service statistics w behan 2014
Biases in irish health service statistics w behan 2014DrWilliamBehan
 
3easywayswebinar
3easywayswebinar3easywayswebinar
3easywayswebinariGive.com
 
Yanni_Cathy_GeneralBiography_711 (1)
Yanni_Cathy_GeneralBiography_711 (1)Yanni_Cathy_GeneralBiography_711 (1)
Yanni_Cathy_GeneralBiography_711 (1)Cathy Yanni
 
Update: David Baker - About CASRAI
Update: David Baker - About CASRAIUpdate: David Baker - About CASRAI
Update: David Baker - About CASRAICASRAI
 
Lt3 d welcome presentation 02 2015
Lt3 d welcome presentation 02 2015Lt3 d welcome presentation 02 2015
Lt3 d welcome presentation 02 2015Stephanie Curtis
 
Workshop Laura Beaupre - Measuring Performance/Quality of Research Administr...
Workshop Laura Beaupre -  Measuring Performance/Quality of Research Administr...Workshop Laura Beaupre -  Measuring Performance/Quality of Research Administr...
Workshop Laura Beaupre - Measuring Performance/Quality of Research Administr...CASRAI
 
σχεδιο υποβολης 2015 ανθρωπινα δικαιωματα
σχεδιο υποβολης 2015 ανθρωπινα δικαιωματασχεδιο υποβολης 2015 ανθρωπινα δικαιωματα
σχεδιο υποβολης 2015 ανθρωπινα δικαιωματαEvi Kamariotaki
 
Crisi= A time of danger "OR" A Time of opportunity
Crisi= A time of danger "OR" A Time of opportunityCrisi= A time of danger "OR" A Time of opportunity
Crisi= A time of danger "OR" A Time of opportunityDavide Geraci
 
Le système de santé à l'épreuve des seniors
Le système de santé à l'épreuve des seniorsLe système de santé à l'épreuve des seniors
Le système de santé à l'épreuve des seniorsJALMAOfficiel
 
ακολουθουν οι 100 καλυτερες ταινιες ολων των εποχων
ακολουθουν οι  100 καλυτερες ταινιες ολων των εποχωνακολουθουν οι  100 καλυτερες ταινιες ολων των εποχων
ακολουθουν οι 100 καλυτερες ταινιες ολων των εποχωνEvi Kamariotaki
 

En vedette (14)

Biases in irish health service statistics w behan 2014
Biases in irish health service statistics w behan 2014Biases in irish health service statistics w behan 2014
Biases in irish health service statistics w behan 2014
 
23-09-2014u3
23-09-2014u323-09-2014u3
23-09-2014u3
 
3easywayswebinar
3easywayswebinar3easywayswebinar
3easywayswebinar
 
шопинг
шопингшопинг
шопинг
 
CDC Embezzlement
CDC EmbezzlementCDC Embezzlement
CDC Embezzlement
 
Yanni_Cathy_GeneralBiography_711 (1)
Yanni_Cathy_GeneralBiography_711 (1)Yanni_Cathy_GeneralBiography_711 (1)
Yanni_Cathy_GeneralBiography_711 (1)
 
Update: David Baker - About CASRAI
Update: David Baker - About CASRAIUpdate: David Baker - About CASRAI
Update: David Baker - About CASRAI
 
Lt3 d welcome presentation 02 2015
Lt3 d welcome presentation 02 2015Lt3 d welcome presentation 02 2015
Lt3 d welcome presentation 02 2015
 
Mainsteam
MainsteamMainsteam
Mainsteam
 
Workshop Laura Beaupre - Measuring Performance/Quality of Research Administr...
Workshop Laura Beaupre -  Measuring Performance/Quality of Research Administr...Workshop Laura Beaupre -  Measuring Performance/Quality of Research Administr...
Workshop Laura Beaupre - Measuring Performance/Quality of Research Administr...
 
σχεδιο υποβολης 2015 ανθρωπινα δικαιωματα
σχεδιο υποβολης 2015 ανθρωπινα δικαιωματασχεδιο υποβολης 2015 ανθρωπινα δικαιωματα
σχεδιο υποβολης 2015 ανθρωπινα δικαιωματα
 
Crisi= A time of danger "OR" A Time of opportunity
Crisi= A time of danger "OR" A Time of opportunityCrisi= A time of danger "OR" A Time of opportunity
Crisi= A time of danger "OR" A Time of opportunity
 
Le système de santé à l'épreuve des seniors
Le système de santé à l'épreuve des seniorsLe système de santé à l'épreuve des seniors
Le système de santé à l'épreuve des seniors
 
ακολουθουν οι 100 καλυτερες ταινιες ολων των εποχων
ακολουθουν οι  100 καλυτερες ταινιες ολων των εποχωνακολουθουν οι  100 καλυτερες ταινιες ολων των εποχων
ακολουθουν οι 100 καλυτερες ταινιες ολων των εποχων
 

Similaire à functions

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).pptxSangeetaBorde3
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Presentation on function
Presentation on functionPresentation on function
Presentation on functionAbu Zaman
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4MKalpanaDevi
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college2017eee0459
 
Functions in C++
Functions in C++Functions in C++
Functions in C++home
 

Similaire à functions (20)

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
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Array Cont
Array ContArray Cont
Array Cont
 
6. function
6. function6. function
6. function
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
UNIT3.pptx
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions
FunctionsFunctions
Functions
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Function
FunctionFunction
Function
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
Function C++
Function C++ Function C++
Function C++
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 

Dernier

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.Kamal Acharya
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Bookingdharasingh5698
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 

Dernier (20)

VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Palanpur 7001035870 Whatsapp Number, 24/07 Booking
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 

functions

  • 1. Sigma institute of engineering SUBJECT: COMPUTER PROGRAMMING AND UTILIZATION PREPARED BY : group c  ENROLLMENT NO:- NAME OF STUDENT 1. 140500119041 KANADE MAHENDRA 2. 140500119052 MAKWANA BHAVESH 3. 140500119054 MEHUL JHALA ASSIGNED BY ; PROF.SURESH CHAUDHARY
  • 3. CONTENTS:  Function  Function example  Vedio on functions  Benefits of function  Math library functions function prototype  Function arguments  C++ variables  Global variable  External variables  Other variables  Recursion  Recursion example
  • 4. Functions  A function is a self contained block of code that performs a particular task.  Any C program can be seen as a collection/group of these functions.  A functions takes some data as input, perform some operation on that data and then return a value.  Any C program must contain at least one function, which is main().  There is no limit on the number of functions that might be present in a C program.
  • 5.  For ex. main() { message(); printf(“I am in main”); } message() { printf(“I am in message”); }
  • 7. Benefits of functions Divide and conquer Manageable program development Software reusability Use existing functions as building blocks for new programs Abstraction - hide internal details (library functions) Avoid code repetition
  • 8. 5.3Math Library Functions  Math library functions  perform common mathematical calculations  #include <math.h>  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
  • 9. Method Description Example ceil( x ) rounds x to the smallest integer not less than x ceil( 9.2 ) is 10.0 ceil( -9.8 ) is -9.0 cos( x ) trigonometric cosine of x (x in radians) cos( 0.0 ) is 1.0 exp( x ) exponential function ex exp( 1.0 ) is 2.71828 exp( 2.0 ) is 7.38906 fabs( x ) absolute value of x fabs( 5.1 ) is 5.1 fabs( 0.0 ) is 0.0 fabs( -8.76 ) is 8.76 floor( x ) rounds x to the largest integer not greater than x floor( 9.2 ) is 9.0 floor( -9.8 ) is -10.0 fmod( x, y ) remainder of x/y as a floating- point number fmod( 13.657, 2.333 ) is 1.992 log( x ) natural logarithm of x (base e) log( 2.718282 ) is 1.0 log( 7.389056 ) is 2.0 log10( x ) logarithm of x (base 10) log10( 10.0 ) is 1.0 log10( 100.0 ) is 2.0 pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128 pow( 9, .5 ) is 3 sin( x ) trigonometric sine of x (x in radians) sin( 0.0 ) is 0 sqrt( x ) square root of x sqrt( 900.0 ) is 30.0 sqrt( 9.0 ) is 3.0 tan( x ) trigonometric tangent of x (x in radians) tan( 0.0 ) is 0 Fig. 3.2 Math library functions. Math Library Functions Revisited
  • 10. 5.2Program Modules in C Functions  Modules in C  Programs combine user-defined functions with library functions  C standard library has a wide variety of functions  Function calls  Invoking functions  Provide function name and arguments (data)  Function performs operations or manipulations  Function returns results  Function call analogy:  Boss asks worker to complete task  Worker gets information, does task, returns result  Information hiding: boss does not know details
  • 11. Function Prototype  All Identifiers in C must be declared before they are used. This is true for functions as well as variables.  For functions, the declarations needs to be done before the first call of the function.  A function declaration specifies the name, return type, and arguments of a function. This is also called the function prototype.  To be a prototype, a function declaration must establish types for the function’s arguments.  Having the prototype available before the first use of the function allows the compiler to check that the correct number and types of arguments are used in the function call.
  • 12.  The prototype has the same syntax as the function definition, except that it is terminated by a semicolon following the closing parenthesis and therefore has no body.  Although, functions that return int values do not require prototypes, prototypes are recommended.
  • 13. Function Definition  General form of any function definition is: return-type function-name(argument declarations) { declarations and statements }  Return-type refers to the data type of the value being returned from the function. If the return type is omitted, int is assumed.  The values provided to a function for processing are the arguments.  The set of statements between the braces is called as the function body.
  • 14. Arguments – Call by Value  In C, all functions are passed “by value” by default.  This means that the called function is given the values of its arguments in temporary variables rather than the originals.  For ex. main() { int a=4,b=5; sum(a,b); printf(“Sum = %d”,a+b); } sum(int a,int b) { a++; b++; printf(“Sum = %d”,a+b); }
  • 15. Arguments – Call by Reference  When necessary, it is possible to arrange a function which can modify a variable in a calling routine.  The caller must provide the address of the variable to be set (generally, a pointer to a variable), and the called function must declare the parameter to be a pointer and access the variable indirectly through it.  For ex: main() { int a=4,b=5; sum(&a,&b); printf(“Sum = %d”,a+b); } sum(int *a,int *b) { (*a)++; (*b)++; printf(“Sum = %d”,(*a)+(*b)); }
  • 16. C++ Variables • A variable is a place in memory that has – A name or identifier (e.g. income, taxes, etc.) – A data type (e.g. int, double, char, etc.) – A size (number of bytes) – A scope (the part of the program code that can use it) • Global variables – all functions can see it and using it • Local variables – only the function that declare local variables see and use these variables – A life time (the duration of its existence) • Global variables can live as long as the program is executed • Local variables are lived only when the functions that define these variables are executed 16
  • 17. Local Variables • Local variables are declared inside the function body and exist as long as the function is running and destroyed when the function exit • You have to initialize the local variable before using it • If a function defines a local variable and there was a global variable with the same name, the function uses its local variable instead of using the global variable 17
  • 18. Example of Defining and Using Global and Local Variables #include <iostream.h> int x; // Global variable Void fun(); // function signature void main() { x = 4; fun(); cout << x << endl; } void fun() { int x = 10; // Local variable cout << x << endl; } 18
  • 19. Local vs Global Variables #include<iostream.h> int x,y; //Global Variables int add2(int, int); //prototype main() { int s; x = 11; y = 22; cout << “global x=” << x << endl; cout << “Global y=” << y << endl; s = add2(x, y); cout << x << “+” << y << “=“ << s; cout<<endl; cout<<“n---end of output---n”; return 0; } int add2(int x1,int y1) { int x; //local variables x=44; cout << “nLocal x=” << x << endl; return x1+y1; } global x=11 global y=22 Local x=44 11+22=33 ---end of output---
  • 20. External Variables  See the below example: main() { extern int a; printf(“Value of a = %d”,a); } int a=5; Output: Value of a = 5
  • 21. Recursion  Recursion defines a function in terms of itself.  It is a programming technique in which a function calls itself.  A recursive function calls itself repeatedly, with different argument values each time.  Some argument values cause the recursive method to return without calling itself. This is the base case.  Either omitting the base case or writing the recursion step incorrectly will cause infinite recursion (stack overflow error).
  • 22.  Recursion to find the factorial of any number: int factorial(int x) { if(x<=1) return 1; else return(x*factorial(x-1)); }
  • 23.  Example: factorials  5! = 5 * 4 * 3 * 2 * 1  Notice that  5! = 5 * 4!  4! = 4 * 3! ...  Can compute factorials recursively  Solve base case (1! = 0! = 1) then plug in  2! = 2 * 1! = 2 * 1 = 2;  3! = 3 * 2! = 3 * 2 = 6;
  • 24. 5.14 Example Using Recursion: The Fibonacci Series  Fibonacci series: 0, 1, 1, 2, 3, 5, 8...  Each number is the sum of the previous two  Can be solved recursively:  fib( n ) = fib( n - 1 ) + fib( n – 2 )  Code for the fibaonacci function long fibonacci( long n ) { if (n == 0 || n == 1) // base case return n; else return fibonacci( n - 1) + fibonacci( n – 2 ); }
  • 25. 5.14 Example Using Recursion: The Fibonacci Series Set of recursive calls to function fibonaccif( 3 ) f( 1 )f( 2 ) f( 1 ) f( 0 ) return 1 return 1 return 0 return + +return
  • 26. 1 /* Fig. 5.15: fig05_15.c 2 Recursive fibonacci function */ 3 #include <stdio.h> 4 5 long fibonacci( long ); 6 7 int main() 8 { 9 long result, number; 10 11 printf( "Enter an integer: " ); 12 scanf( "%ld", &number ); 13 result = fibonacci( number ); 14 printf( "Fibonacci( %ld ) = %ldn", number, result ); 15 return 0; 16 } 17 18 /* Recursive definition of function fibonacci */ 19 long fibonacci( long n ) 20 { 21 if ( n == 0 || n == 1 ) 22 return n; 23 else 24 return fibonacci( n - 1 ) + fibonacci( n - 2 ); 25 }
  • 27. output: Enter an integer: 0 Fibonacci(0) = 0 Enter an integer: 1 Fibonacci(1) = 1  Program Output Enter an integer: 2 Fibonacci(2) = 1 Enter an integer: 3 Fibonacci(3) = 2 Enter an integer: 4 Fibonacci(4) = 3 Enter an integer: 5 Fibonacci(5) = 5 Enter an integer: 6 Fibonacci(6) = 8 Enter an integer: 10 Fibonacci(10) = 55 Enter an integer: 20 Fibonacci(20) = 6765 Enter an integer: 30 Fibonacci(30) = 832040 Enter an integer: 35 Fibonacci(35) = 9227465