SlideShare a Scribd company logo
Multidimensional Arrays
• Define multidimensional arrays as array of arrays.
• Data in multidimensional arrays are stored in tabular form (in row major
order).
Syntax
data_type array_name[size1][size2]....[sizeN];
• Three dimensional array: int three_d[10][20][30];
• Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.
Functions
• Initializing & Accessing 3-DArray:
– Initialization & Accessing is same as that of 2D arrays.
– The difference is the number of dimension increases so the number of
nested braces and number of loops will also increase.
Method 1:
int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23};
Better Method:
int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }, { {12,13,14,15},
{16,17,18,19}, {20,21,22,23} } };
Functions
Functions
• A function is a block of code that performs a specific task.
• A function is a self contained block of statements that perform a logical
task of some kind.
• C allows the user to define functions according to our need. These
functions are known as user-defined functions.
• Complex Large problem is easier to solve by dividing it into several smaller,
self-contained parts that perform a specific task called Functions ---
Modularization Concept
Functions
Advantages of Functions
• Logical clarity – decomposing a program into several concise functions
– Makes program easier to understand.
– Easier to write
– Easier to debug
• Separates the concept (what is done) from the implementation (how it is
done).
• Avoids (redundant) repeated programming of same instructions
Repeated Instructions are placed inside function
Access to function many times with different data when needed
• To build customized library of frequently used routines contains system-
dependent features
• Portability – programs are written that are independent of system-
dependent features
Introduction
• One important function is main()
• Execution always begin and end by instructions in main()
• Additional functions will be subordinate to main
• Sub Function definitions may appear in any order
• One function cannot be embedded within another
• Same function can be called from different places of program
• C supports lot of predefined functions known as Library Functions
• User is free to define its own functions and use it called as user-defined
functions
– Function Body
– Function Call
– Function Prototype
• Information is passed to the function via special identifiers called
arguments (parameters)
• Information is returned via return statement
Defining a Function/ Function Definition
• It contains block of code to perform a specific task
• When a function is called, the compiler starts
executing the codes inside the body of a function.
Syntax:
returnType functionName(type1 arg1, type2 arg2, ...)
{
//body of the function
}
• The first line is known as function header and the statements within the
opening and closing braces is known as the function body.
• Function header:-
– Consists of return type, the function name, and the formal
parameter list.
– Function name is any valid identifier
– Return type specifies the type of value that the function is expected to
return to the program calling the function. By default it is integer type.
– The parameter list called formal parameters declares the variables that
will receive the data sent by the calling program.
– Formal parameters are local to function
• Function body:-
– Contains the declaration and executable statements necessary for
performing the required task.
– It is enclosed with open and closed braces
void sum(int a, int b)
{
int c;
c = a + b;
printf(“Sum = %d”,c);
}
display()
{
printf(“Welcome”);
}
//By default, return type of the function is int
display()
{
printf(“Welcome”);
return;
}
Function Call/ Accessing a Function
• A function can be called by using its name followed by a list of
actual arguments.
• During function call, control of the program is transferred to the
user-defined function
• It is the statement inside the main( ) or sub function
Syntax:
functionName(arg1, arg2, ...);
• Here, value of actual argument is transferred into function and
assigned to formal argument
add(2,3);
display();
FUNCTION PROTOTYPE
• It is the declaration of a function
• It specifies function's name, parameters and return type
• It doesn't contain function body
• It gives information to the compiler that the function may later be used
in the program.
• It is usually written in the beginning of the program
Syntax:
returnType functionName(type1 arg1, type2 arg2,...);
• It is not needed if the function is defined before the main( ) function.
• C prefers top-down approach.
• Argument names can be omitted since these are “dummy arguments”
that are not used inside our programs
• Function Prototypes are not mandatory in C
void add(int, int);
void add(int x, int y);
int display();
void display();
#include<stdion.h>
void add(int, int); //Function Prototype
void main()
{
int a=10, b=30;
add(a,b); //Function Call
}
//Function Definition
void add(int x,int y) //Function Header
{
int sum; // Function Body
sum = x + y; “
printf(“Sum = %d”,sum); “
}
#include<stdion.h>
//Function Definition
void add(int x,int y) //Function Header
{
int sum; // Function Body
sum = x + y; “
printf(“Sum = %d”,sum); “
}
void main()
{
int a=10, b=30;
add(a,b); //Function Call
}
• Actual arguments:
– Arguments passed in a function call are called actual arguments.
– These arguments are defined in the calling function.
• Formal arguments:
– Parameters/arguments in a function definition
– Scope of formal arguments is local to the function definition in
which they are used.
– Belongs to the called function
– Copy of the actual arguments.
– A change in formal arguments would not be reflected in the
actual arguments.
PASSING ARGUMENTS TO FUNCTIONS
• Argument refers to the variable passed to the function.
• In the above example, two variables a and b are passed during function
call. These arguments are called actual arguments
• The parameters x and y in function definition accepts the passed
arguments. These arguments are called formal parameters.
• We can pass arguments by value – Copies of data passed
• We can pass arguments by reference – Original copy of data is passed
• We can pass array to function – address of first array element is passed
Functions
Call by Value vs. Call by Reference
Call by Value Call by Reference
This is the usual method to call a function in
which value of the variable is passed as an
argument
In this method, the address of the variable
is passed as an argument
Any alternation in the value of the argument
passed is local to the function and is not
accepted in the calling program
Any alternation in the value of the
argument passed is accepted in the calling
program
Memory location occupied by formal and
actual arguments values are different
Memory location occupied by formal and
actual arguments values are same
Since a new location is created, this method
is slow
Since the existing memory location is used
through its address, this method is fast
There is no possibility of wrong data
manipulation since the arguments are
directly used in an application
There is a possibility of wrong data
manipulation since the addresses are used
in an expression. A good skill of
programming is required here
• Example Program:
– Swap Two Numbers using Call by Value
– Swap Two Numbers using Call by Reference
Passing Array to a function
• To pass an entire array to a function, only the name of the array is passed as an
argument.
functionname(arrayname); //Function Call
• Define function in one of the ways:
• First way:
return_type function(type arrayname[])
Declaring blank subscript notation [] is the widely used technique.
• Second way:
return_type function(type arrayname[SIZE])
Optionally, we can define size in subscript notation [].
• Third way:
return_type function(type *arrayname)
• Elements are not passed to function
• Passes the address of the first array element
• Now, formal argument becomes the pointer to the first array element
• Example Program:
– Search an element in an array using functions
RETURN STATEMENT
• The return statement terminates the execution of a function and returns a
value to the calling function.
• The program control is transferred to the calling function after return
statement.
Syntax:
return (expression);
Example:
return a;
return (a+b);
• The type of value returned from the function and the return type specified
in function prototype and function definition must match.
• The value of variable result is returned to the variable sum in the main( )
function.
• Program can have number of return statements.
• main() returns 0 or 1 to OS by default: 0 --- successful completion, 1 –
Unsuccessful/ Interruption
• Sub function returns 0 or 1 to the calling function: 0 – unsuccessful, 1 –
successful execution
void greatest(int a,int b)
{
if (a > b)
return a;
else
return b;
}
• User-defined functions can be categorized as:
– Function with no arguments and no return value
– Function with no arguments and a return value
– Function with arguments and no return value
– Function with arguments and a return value
1) No arguments passed and no
return Value
#include <stdio.h>
void addNumbers( ); // function prototype
void main()
{
addNumbers( ); // function call without args & return
}
void addNumbers( ) // function definition
{
int n1,n2, sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = n1+n2;
printf("sum = %d",sum);
}
• The addNumbers( ) function takes no arguments and doesn’t return any
value
• The empty parentheses in addNumbers( ); statement inside the main()
function indicates that no argument is passed to the function.
• The return type of the function is void. Hence, no value is returned from
the function.
2) No arguments passed but a return
value
#include <stdio.h>
int addNumbers( ); // function prototype
void main()
{
sum = addNumbers( ); // function call with return
printf("sum = %d",sum);
}
int addNumbers( ) // function definition
{
int n1,n2, result;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
result = n1+n2;
return result; // return statement
}
• The empty parentheses in sum = addNumbers( ); statement indicates that
no argument is passed to the function.
• The value returned from the function is assigned to sum.
• Here, the addNumbers( ) function takes input from the user and returns
resultant value to the main( ).
3) Argument passed but no return
value
#include <stdio.h>
void addNumbers(int a, int b); // function prototype
void main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
addNumbers(n1, n2); // function call with arguments
}
void addNumbers(int a,int b) // function definition
{
int result;
result = a+b;
printf("sum = %d",result); //No Return Statement
}
• The integer values entered by the user is passed to addNumbers(n1,n2)
function.
• This function calculates and prints the result.
4) Argument passed and a return
value
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
void main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
}
int addNumbers(int a,int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
• The input from the user is passed to function.
• The function checks calculates and returns the result
• Then, the result is displayed from the main() function
RECURSION FUCTION
• A function that calls itself is known as a recursive function. And, this
technique is known as recursion.
• The recursion continues until some condition is met to prevent it.
• To prevent infinite recursion, if...else statement can be used where one
branch makes the recursive call and other doesn't.
• A recursive function performs the tasks by dividing it into the subtasks.
• Recursion program consists of two parts:
– Base case: It is a termination condition defined in the
function. It stops the recursion. It returns the final result.
– Recursive case: where the function keeps calling itself to
perform a subtask
Functions
Example: Factorial Calculation using recursion
long fact (int);
void main()
{
int n;
long f;
printf("Enter the number : ");
scanf("%d“,&n);
f = fact(n);
printf(“Factorial of %d is %ld",n, f);
}
long fact(int n)
{
if (n==0)
return 0;
else
return n*fact(n-1);
}
Output
Enter the number : 5
Factorial of 5 is 120
Ad

More Related Content

What's hot (20)

Function in C Language
Function in C Language Function in C Language
Function in C Language
programmings guru
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
Maliha Mehr
 
Encoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexersEncoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexers
pubgalarab
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
sangrampatil81
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
SKUP1
 
Introduction to Interfacing Technique
Introduction to Interfacing TechniqueIntroduction to Interfacing Technique
Introduction to Interfacing Technique
Mabeth MaRiyah Ramos
 
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
 
C pointer
C pointerC pointer
C pointer
University of Potsdam
 
Pointers in C
Pointers in CPointers in C
Pointers in C
Monishkanungo
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
RamaBoya2
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
Haard Shah
 
Karnaugh maps
Karnaugh mapsKarnaugh maps
Karnaugh maps
AJAL A J
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
Wingston
 
1.ripple carry adder, full adder implementation using half adder.
1.ripple carry adder, full adder implementation using half adder.1.ripple carry adder, full adder implementation using half adder.
1.ripple carry adder, full adder implementation using half adder.
MdFazleRabbi18
 
Computer architecture addressing modes and formats
Computer architecture addressing modes and formatsComputer architecture addressing modes and formats
Computer architecture addressing modes and formats
Mazin Alwaaly
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
Computer arithmetic
Computer arithmeticComputer arithmetic
Computer arithmetic
Balakrishna Chowdary
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 
Encoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexersEncoder, decoder, multiplexers and demultiplexers
Encoder, decoder, multiplexers and demultiplexers
pubgalarab
 
Scope rules : local and global variables
Scope rules : local and global variablesScope rules : local and global variables
Scope rules : local and global variables
sangrampatil81
 
C-Programming Function pointers.pptx
C-Programming  Function pointers.pptxC-Programming  Function pointers.pptx
C-Programming Function pointers.pptx
SKUP1
 
Introduction to Interfacing Technique
Introduction to Interfacing TechniqueIntroduction to Interfacing Technique
Introduction to Interfacing Technique
Mabeth MaRiyah Ramos
 
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
 
Fundamentals of C Programming Language
Fundamentals of C Programming LanguageFundamentals of C Programming Language
Fundamentals of C Programming Language
RamaBoya2
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
Haard Shah
 
Karnaugh maps
Karnaugh mapsKarnaugh maps
Karnaugh maps
AJAL A J
 
C programming - Pointers
C programming - PointersC programming - Pointers
C programming - Pointers
Wingston
 
1.ripple carry adder, full adder implementation using half adder.
1.ripple carry adder, full adder implementation using half adder.1.ripple carry adder, full adder implementation using half adder.
1.ripple carry adder, full adder implementation using half adder.
MdFazleRabbi18
 
Computer architecture addressing modes and formats
Computer architecture addressing modes and formatsComputer architecture addressing modes and formats
Computer architecture addressing modes and formats
Mazin Alwaaly
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
C Programming Storage classes, Recursion
C Programming Storage classes, RecursionC Programming Storage classes, Recursion
C Programming Storage classes, Recursion
Sreedhar Chowdam
 
C Programming: Structure and Union
C Programming: Structure and UnionC Programming: Structure and Union
C Programming: Structure and Union
Selvaraj Seerangan
 
Union in C programming
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya
 

Similar to Functions (20)

CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
PSPC-UNIT-4.pdf
PSPC-UNIT-4.pdfPSPC-UNIT-4.pdf
PSPC-UNIT-4.pdf
ArshiniGubbala3
 
Function
Function Function
Function
Kathmandu University
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
4th unit full
4th unit full4th unit full
4th unit full
Murali Saktheeswaran
 
Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
 
Functions
Functions Functions
Functions
Dr.Subha Krishna
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvdFunctions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
scs150831
 
Functions
FunctionsFunctions
Functions
Pragnavi Erva
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
 
arrays.ppt
arrays.pptarrays.ppt
arrays.ppt
Bharath904863
 
Function
FunctionFunction
Function
Saniati
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Anil Pokhrel
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
LadallaRajKumar
 
Functions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions ConceptsFunctions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions Concepts
nitinaees
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Unit 7. Functions
Unit 7. FunctionsUnit 7. Functions
Unit 7. Functions
Ashim Lamichhane
 
CH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptxCH.4FUNCTIONS IN C (1).pptx
CH.4FUNCTIONS IN C (1).pptx
sangeeta borde
 
CHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptxCHAPTER THREE FUNCTION.pptx
CHAPTER THREE FUNCTION.pptx
GebruGetachew2
 
16717 functions in C++
16717 functions in C++16717 functions in C++
16717 functions in C++
LPU
 
Funtions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the topsFuntions of c programming. the functions of c helps to clarify all the tops
Funtions of c programming. the functions of c helps to clarify all the tops
sameermhr345
 
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvdFunctions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
Functions in C-csvsvvcvxcvxvxcvxcvvsvsvsfvsfvd
scs150831
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
Ashwini Raut
 
Function Overloading Call by value and call by reference
Function Overloading Call by value and call by referenceFunction Overloading Call by value and call by reference
Function Overloading Call by value and call by reference
TusharAneyrao1
 
Function in C Programming
Function in C ProgrammingFunction in C Programming
Function in C Programming
Anil Pokhrel
 
Functions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions ConceptsFunctions in Pythons UDF and Functions Concepts
Functions in Pythons UDF and Functions Concepts
nitinaees
 
Ad

Recently uploaded (20)

Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
Sinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_NameSinhala_Male_Names.pdf Sinhala_Male_Name
Sinhala_Male_Names.pdf Sinhala_Male_Name
keshanf79
 
How to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odooHow to Set warnings for invoicing specific customers in odoo
How to Set warnings for invoicing specific customers in odoo
Celine George
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Ad

Functions

  • 1. Multidimensional Arrays • Define multidimensional arrays as array of arrays. • Data in multidimensional arrays are stored in tabular form (in row major order). Syntax data_type array_name[size1][size2]....[sizeN]; • Three dimensional array: int three_d[10][20][30]; • Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.
  • 3. • Initializing & Accessing 3-DArray: – Initialization & Accessing is same as that of 2D arrays. – The difference is the number of dimension increases so the number of nested braces and number of loops will also increase. Method 1: int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; Better Method: int x[2][3][4] = { { {0,1,2,3}, {4,5,6,7}, {8,9,10,11} }, { {12,13,14,15}, {16,17,18,19}, {20,21,22,23} } };
  • 5. Functions • A function is a block of code that performs a specific task. • A function is a self contained block of statements that perform a logical task of some kind. • C allows the user to define functions according to our need. These functions are known as user-defined functions. • Complex Large problem is easier to solve by dividing it into several smaller, self-contained parts that perform a specific task called Functions --- Modularization Concept
  • 7. Advantages of Functions • Logical clarity – decomposing a program into several concise functions – Makes program easier to understand. – Easier to write – Easier to debug • Separates the concept (what is done) from the implementation (how it is done). • Avoids (redundant) repeated programming of same instructions Repeated Instructions are placed inside function Access to function many times with different data when needed • To build customized library of frequently used routines contains system- dependent features • Portability – programs are written that are independent of system- dependent features
  • 8. Introduction • One important function is main() • Execution always begin and end by instructions in main() • Additional functions will be subordinate to main • Sub Function definitions may appear in any order • One function cannot be embedded within another • Same function can be called from different places of program • C supports lot of predefined functions known as Library Functions • User is free to define its own functions and use it called as user-defined functions – Function Body – Function Call – Function Prototype • Information is passed to the function via special identifiers called arguments (parameters) • Information is returned via return statement
  • 9. Defining a Function/ Function Definition • It contains block of code to perform a specific task • When a function is called, the compiler starts executing the codes inside the body of a function. Syntax: returnType functionName(type1 arg1, type2 arg2, ...) { //body of the function }
  • 10. • The first line is known as function header and the statements within the opening and closing braces is known as the function body. • Function header:- – Consists of return type, the function name, and the formal parameter list. – Function name is any valid identifier – Return type specifies the type of value that the function is expected to return to the program calling the function. By default it is integer type. – The parameter list called formal parameters declares the variables that will receive the data sent by the calling program. – Formal parameters are local to function • Function body:- – Contains the declaration and executable statements necessary for performing the required task. – It is enclosed with open and closed braces
  • 11. void sum(int a, int b) { int c; c = a + b; printf(“Sum = %d”,c); }
  • 12. display() { printf(“Welcome”); } //By default, return type of the function is int display() { printf(“Welcome”); return; }
  • 13. Function Call/ Accessing a Function • A function can be called by using its name followed by a list of actual arguments. • During function call, control of the program is transferred to the user-defined function • It is the statement inside the main( ) or sub function Syntax: functionName(arg1, arg2, ...); • Here, value of actual argument is transferred into function and assigned to formal argument
  • 15. FUNCTION PROTOTYPE • It is the declaration of a function • It specifies function's name, parameters and return type • It doesn't contain function body • It gives information to the compiler that the function may later be used in the program. • It is usually written in the beginning of the program Syntax: returnType functionName(type1 arg1, type2 arg2,...); • It is not needed if the function is defined before the main( ) function. • C prefers top-down approach. • Argument names can be omitted since these are “dummy arguments” that are not used inside our programs • Function Prototypes are not mandatory in C
  • 16. void add(int, int); void add(int x, int y); int display(); void display();
  • 17. #include<stdion.h> void add(int, int); //Function Prototype void main() { int a=10, b=30; add(a,b); //Function Call } //Function Definition void add(int x,int y) //Function Header { int sum; // Function Body sum = x + y; “ printf(“Sum = %d”,sum); “ }
  • 18. #include<stdion.h> //Function Definition void add(int x,int y) //Function Header { int sum; // Function Body sum = x + y; “ printf(“Sum = %d”,sum); “ } void main() { int a=10, b=30; add(a,b); //Function Call }
  • 19. • Actual arguments: – Arguments passed in a function call are called actual arguments. – These arguments are defined in the calling function. • Formal arguments: – Parameters/arguments in a function definition – Scope of formal arguments is local to the function definition in which they are used. – Belongs to the called function – Copy of the actual arguments. – A change in formal arguments would not be reflected in the actual arguments.
  • 20. PASSING ARGUMENTS TO FUNCTIONS • Argument refers to the variable passed to the function. • In the above example, two variables a and b are passed during function call. These arguments are called actual arguments • The parameters x and y in function definition accepts the passed arguments. These arguments are called formal parameters. • We can pass arguments by value – Copies of data passed • We can pass arguments by reference – Original copy of data is passed • We can pass array to function – address of first array element is passed
  • 22. Call by Value vs. Call by Reference Call by Value Call by Reference This is the usual method to call a function in which value of the variable is passed as an argument In this method, the address of the variable is passed as an argument Any alternation in the value of the argument passed is local to the function and is not accepted in the calling program Any alternation in the value of the argument passed is accepted in the calling program Memory location occupied by formal and actual arguments values are different Memory location occupied by formal and actual arguments values are same Since a new location is created, this method is slow Since the existing memory location is used through its address, this method is fast There is no possibility of wrong data manipulation since the arguments are directly used in an application There is a possibility of wrong data manipulation since the addresses are used in an expression. A good skill of programming is required here
  • 23. • Example Program: – Swap Two Numbers using Call by Value – Swap Two Numbers using Call by Reference
  • 24. Passing Array to a function • To pass an entire array to a function, only the name of the array is passed as an argument. functionname(arrayname); //Function Call • Define function in one of the ways: • First way: return_type function(type arrayname[]) Declaring blank subscript notation [] is the widely used technique. • Second way: return_type function(type arrayname[SIZE]) Optionally, we can define size in subscript notation []. • Third way: return_type function(type *arrayname) • Elements are not passed to function • Passes the address of the first array element • Now, formal argument becomes the pointer to the first array element
  • 25. • Example Program: – Search an element in an array using functions
  • 26. RETURN STATEMENT • The return statement terminates the execution of a function and returns a value to the calling function. • The program control is transferred to the calling function after return statement. Syntax: return (expression); Example: return a; return (a+b); • The type of value returned from the function and the return type specified in function prototype and function definition must match.
  • 27. • The value of variable result is returned to the variable sum in the main( ) function. • Program can have number of return statements. • main() returns 0 or 1 to OS by default: 0 --- successful completion, 1 – Unsuccessful/ Interruption • Sub function returns 0 or 1 to the calling function: 0 – unsuccessful, 1 – successful execution
  • 28. void greatest(int a,int b) { if (a > b) return a; else return b; }
  • 29. • User-defined functions can be categorized as: – Function with no arguments and no return value – Function with no arguments and a return value – Function with arguments and no return value – Function with arguments and a return value
  • 30. 1) No arguments passed and no return Value #include <stdio.h> void addNumbers( ); // function prototype void main() { addNumbers( ); // function call without args & return } void addNumbers( ) // function definition { int n1,n2, sum; printf("Enters two numbers: "); scanf("%d %d",&n1,&n2); sum = n1+n2; printf("sum = %d",sum); }
  • 31. • The addNumbers( ) function takes no arguments and doesn’t return any value • The empty parentheses in addNumbers( ); statement inside the main() function indicates that no argument is passed to the function. • The return type of the function is void. Hence, no value is returned from the function.
  • 32. 2) No arguments passed but a return value #include <stdio.h> int addNumbers( ); // function prototype void main() { sum = addNumbers( ); // function call with return printf("sum = %d",sum); } int addNumbers( ) // function definition { int n1,n2, result; printf("Enters two numbers: "); scanf("%d %d",&n1,&n2); result = n1+n2; return result; // return statement }
  • 33. • The empty parentheses in sum = addNumbers( ); statement indicates that no argument is passed to the function. • The value returned from the function is assigned to sum. • Here, the addNumbers( ) function takes input from the user and returns resultant value to the main( ).
  • 34. 3) Argument passed but no return value #include <stdio.h> void addNumbers(int a, int b); // function prototype void main() { int n1,n2,sum; printf("Enters two numbers: "); scanf("%d %d",&n1,&n2); addNumbers(n1, n2); // function call with arguments } void addNumbers(int a,int b) // function definition { int result; result = a+b; printf("sum = %d",result); //No Return Statement }
  • 35. • The integer values entered by the user is passed to addNumbers(n1,n2) function. • This function calculates and prints the result.
  • 36. 4) Argument passed and a return value #include <stdio.h> int addNumbers(int a, int b); // function prototype void main() { int n1,n2,sum; printf("Enters two numbers: "); scanf("%d %d",&n1,&n2); sum = addNumbers(n1, n2); // function call printf("sum = %d",sum); } int addNumbers(int a,int b) // function definition { int result; result = a+b; return result; // return statement }
  • 37. • The input from the user is passed to function. • The function checks calculates and returns the result • Then, the result is displayed from the main() function
  • 38. RECURSION FUCTION • A function that calls itself is known as a recursive function. And, this technique is known as recursion. • The recursion continues until some condition is met to prevent it. • To prevent infinite recursion, if...else statement can be used where one branch makes the recursive call and other doesn't. • A recursive function performs the tasks by dividing it into the subtasks.
  • 39. • Recursion program consists of two parts: – Base case: It is a termination condition defined in the function. It stops the recursion. It returns the final result. – Recursive case: where the function keeps calling itself to perform a subtask
  • 41. Example: Factorial Calculation using recursion long fact (int); void main() { int n; long f; printf("Enter the number : "); scanf("%d“,&n); f = fact(n); printf(“Factorial of %d is %ld",n, f); } long fact(int n) { if (n==0) return 0; else return n*fact(n-1); } Output Enter the number : 5 Factorial of 5 is 120