SlideShare a Scribd company logo
Function
Khandana ali khan
Date: 25, April 2019
Function
• A large C program is divided into basic building blocks called C
function.
• Modules in C are called functions.
• A function is a group of statements that together perform a task.
• Every C program has at least one function, which is main()
• You can divide up your code into separate functions. How you divide
up your code among different functions is up to you, but logically the
division is such that each function performs a specific task.
• Reduce the complexity
• Reusability
Hierarchical function
Types of Function
• In C Programming there are two types of c functions
1. Standard function
• A function which is predefined in c programming is called a standard
function. main() , printf (), scanf() functions are standard function
2. User-defined function
• User-defined function are the functions which are defined by the user
C FUNCTION DECLARATION, FUNCTION CALL AND
FUNCTION DEFINITION
• There are 3 aspects in each C function.
1. Function declaration or prototype
– This informs compiler about the function name, function parameters
and return value’s data type.
2. Function call
– This calls the actual function
3. Function definition
– This contains all the statements to be executed.
C function Syntax
C functions aspects Syntax
function definition Return_type function_name (arguments list)
{ Body of function; }
function call function_name (arguments list);
function declaration return_type function_name (argument list);
Parameters
1. Actual parameter
– This is the argument which is used in function call.
2. Formal parameter
• – This is the argument which is used in function definition
Function program
• #include <stdio.h>
• int square( int y ); // function prototype
• int main()
• {
• int x; // counter
• // loop 10 times and calculate and output square of x each time
• for ( x = 1; x <= 10; ++x )
• {
• printf( "%d ", square( x )
• ); // function call
• } // end for
• printf( "" );
}
• // square function definition returns the square of its parameter
• int square( int y ) // y is a copy of the argument to the function
• {
• return y * y; // returns the square of y as an int
• } // end function square
Types of Function / C Argument, return value
• C function with arguments (parameters) and with return value.
• C function with arguments (parameters) and without return value.
• C function without arguments (parameters) and without return value.
• C function without arguments (parameters) and with return value
HOW TO CALL C FUNCTIONS IN A PROGRAM?
• There are two ways that a C function can be called from a program. They are,
1. CALL BY VALUE:
• In call by value method, the value of the variable is passed to the function as parameter.
• The value of the actual parameter can not be modified by formal parameter.
• Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is
copied to formal parameter.
2.CALL BY REFERENCE:
• In call by reference method, the address of the variable is passed to the function as parameter.
• The value of the actual parameter can be modified by formal parameter.
• Same memory is used for both actual and formal parameters since only address is used by both
parameters.
Call by Value
• #include <stdio.h>
• void swap(int x, int y); /* function declaration */
• int main () {
• int a = 100; /* local variable definition */
• int b = 200;
• printf("Before swap, value of a : %dn", a );
• printf("Before swap, value of b : %dn", b );
• /* calling a function to swap the values */
• swap(a, b);
• printf("After swap, value of a : %dn", a );
• printf("After swap, value of b : %dn", b );
• return 0; }
• /* function definition to swap the values */
• void swap(int x, int y) {
• int temp;
• temp = x; /* save the value of x */
• x = y; /* put y into x */
• y = temp; /* put temp into y */
• return; }
Call by Reference
• #include <stdio.h>
• void swap(int *x, int *y); /* function declaration */
• int main (){
• int a = 100; /* local variable definition */
• int b = 200;
• printf("Before swap, value of a : %dn", a );
• printf("Before swap, value of b : %dn", b );
• /* calling a function to swap the values.
• * &a indicates pointer to a i.e. address of variable a and
• * &b indicates pointer to b i.e. address of variable b.
• */
• swap(&a, &b);
• printf("After swap, value of a : %dn", a );
• printf("After swap, value of b : %dn", b );
• return 0;
• }/* function definition to swap the values */
• void swap(int *x, int *y)
• {
• int temp;
• temp = *x; /* save the value at address x */
• *x = *y; /* put y into x */
• *y = temp; /* put temp into y */
• Return;
• }
• /* function definition to swap the values */
• void swap(int x, int y)
• {
• int temp;
• temp = x; /* save the value of x */
• x = y; /* put y into x */
• y = temp; /* put temp into y */
• return;
• }
• #include <stdio.h>
• /* function declaration */
• void swap(int x, int y);
• int main ()
• {
• /* local variable definition */
• int a = 100;
• int b = 200;
• printf("Before swap, value of a : %dn", a );
• printf("Before swap, value of b : %dn", b );
• /* calling a function to swap the values */
• swap(a, b);
• printf("After swap, value of a : %dn", a );
• printf("After swap, value of b : %dn", b );
• return 0;
• }

More Related Content

PPSX
C programming function
PPTX
parameter passing in c#
PPTX
C Programming Language Step by Step Part 2
PPT
16717 functions in C++
 
PPTX
Functions in C
PPSX
Functions in c
PPTX
Call by value or call by reference in C++
PPT
Functions in C++
C programming function
parameter passing in c#
C Programming Language Step by Step Part 2
16717 functions in C++
 
Functions in C
Functions in c
Call by value or call by reference in C++
Functions in C++

What's hot (20)

PPTX
Call by value
PPT
lets play with "c"..!!! :):)
PPT
Lecture#7 Call by value and reference in c++
PPT
Lecture#6 functions in c++
PPT
Functions in C++
PPT
Function overloading(C++)
PPTX
C and C++ functions
PPTX
Function C++
PPTX
Functions in C++
PPT
C++ Function
PPTX
C++ programming function
PPTX
functions of C++
PPTX
Presentation on function
PPTX
Function in c
ODP
Function
PPSX
Function in c
PPT
Introduction to c
PPTX
PPTX
Types of function call
PDF
Function lecture
Call by value
lets play with "c"..!!! :):)
Lecture#7 Call by value and reference in c++
Lecture#6 functions in c++
Functions in C++
Function overloading(C++)
C and C++ functions
Function C++
Functions in C++
C++ Function
C++ programming function
functions of C++
Presentation on function
Function in c
Function
Function in c
Introduction to c
Types of function call
Function lecture
Ad

Similar to Function (rule in programming) (20)

PPTX
Detailed concept of function in c programming
PPTX
Presentation on Function in C Programming
PPTX
CH.4FUNCTIONS IN C (1).pptx
PPTX
C functions by ranjan call by value and reference.pptx
PPTX
Function C programming
PDF
programlama fonksiyonlar c++ hjhjghjv jg
PPT
Functions and pointers_unit_4
PPTX
PPTX
unit_2.pptx
PPTX
C Programming Language Part 7
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PPTX
C concepts and programming examples for beginners
PPTX
unit_2 (1).pptx
PDF
cp Module4(1)
PPT
presentation_functions_1443207686_140676.ppt
PPT
User Defined Functions in C
PDF
Principals of Programming in CModule -5.pdfModule-3.pdf
PPTX
UNIT3.pptx
PPT
C programming is a powerful, general-purpose language used for developing ope...
Detailed concept of function in c programming
Presentation on Function in C Programming
CH.4FUNCTIONS IN C (1).pptx
C functions by ranjan call by value and reference.pptx
Function C programming
programlama fonksiyonlar c++ hjhjghjv jg
Functions and pointers_unit_4
unit_2.pptx
C Programming Language Part 7
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
C concepts and programming examples for beginners
unit_2 (1).pptx
cp Module4(1)
presentation_functions_1443207686_140676.ppt
User Defined Functions in C
Principals of Programming in CModule -5.pdfModule-3.pdf
UNIT3.pptx
C programming is a powerful, general-purpose language used for developing ope...
Ad

More from Unviersity of balochistan quetta (10)

PPTX
Most difficult language presentation
DOCX
Let's us c language (sabeel Bugti)
PPTX
PPTX
Introduction to psychology
PPT
PPTX
Sensation and perception.pptm
Most difficult language presentation
Let's us c language (sabeel Bugti)
Introduction to psychology
Sensation and perception.pptm

Recently uploaded (20)

PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
From loneliness to social connection charting
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Revamp in MTO Odoo 18 Inventory - Odoo Slides
O7-L3 Supply Chain Operations - ICLT Program
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Open Quiz Monsoon Mind Game Final Set.pptx
Open Quiz Monsoon Mind Game Prelims.pptx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Electrolyte Disturbances and Fluid Management A clinical and physiological ap...
human mycosis Human fungal infections are called human mycosis..pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
O5-L3 Freight Transport Ops (International) V1.pdf
From loneliness to social connection charting
Abdominal Access Techniques with Prof. Dr. R K Mishra
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
How to Manage Starshipit in Odoo 18 - Odoo Slides
PSYCHOLOGY IN EDUCATION.pdf ( nice pdf ...)

Function (rule in programming)

  • 2. Function • A large C program is divided into basic building blocks called C function. • Modules in C are called functions. • A function is a group of statements that together perform a task. • Every C program has at least one function, which is main() • You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task. • Reduce the complexity • Reusability
  • 4. Types of Function • In C Programming there are two types of c functions 1. Standard function • A function which is predefined in c programming is called a standard function. main() , printf (), scanf() functions are standard function 2. User-defined function • User-defined function are the functions which are defined by the user
  • 5. C FUNCTION DECLARATION, FUNCTION CALL AND FUNCTION DEFINITION • There are 3 aspects in each C function. 1. Function declaration or prototype – This informs compiler about the function name, function parameters and return value’s data type. 2. Function call – This calls the actual function 3. Function definition – This contains all the statements to be executed.
  • 6. C function Syntax C functions aspects Syntax function definition Return_type function_name (arguments list) { Body of function; } function call function_name (arguments list); function declaration return_type function_name (argument list);
  • 7. Parameters 1. Actual parameter – This is the argument which is used in function call. 2. Formal parameter • – This is the argument which is used in function definition
  • 8. Function program • #include <stdio.h> • int square( int y ); // function prototype • int main() • { • int x; // counter • // loop 10 times and calculate and output square of x each time • for ( x = 1; x <= 10; ++x ) • { • printf( "%d ", square( x ) • ); // function call • } // end for • printf( "" ); } • // square function definition returns the square of its parameter • int square( int y ) // y is a copy of the argument to the function • { • return y * y; // returns the square of y as an int • } // end function square
  • 9. Types of Function / C Argument, return value • C function with arguments (parameters) and with return value. • C function with arguments (parameters) and without return value. • C function without arguments (parameters) and without return value. • C function without arguments (parameters) and with return value
  • 10. HOW TO CALL C FUNCTIONS IN A PROGRAM? • There are two ways that a C function can be called from a program. They are, 1. CALL BY VALUE: • In call by value method, the value of the variable is passed to the function as parameter. • The value of the actual parameter can not be modified by formal parameter. • Different Memory is allocated for both actual and formal parameters. Because, value of actual parameter is copied to formal parameter. 2.CALL BY REFERENCE: • In call by reference method, the address of the variable is passed to the function as parameter. • The value of the actual parameter can be modified by formal parameter. • Same memory is used for both actual and formal parameters since only address is used by both parameters.
  • 11. Call by Value • #include <stdio.h> • void swap(int x, int y); /* function declaration */ • int main () { • int a = 100; /* local variable definition */ • int b = 200; • printf("Before swap, value of a : %dn", a ); • printf("Before swap, value of b : %dn", b ); • /* calling a function to swap the values */ • swap(a, b); • printf("After swap, value of a : %dn", a ); • printf("After swap, value of b : %dn", b ); • return 0; } • /* function definition to swap the values */ • void swap(int x, int y) { • int temp; • temp = x; /* save the value of x */ • x = y; /* put y into x */ • y = temp; /* put temp into y */ • return; }
  • 12. Call by Reference • #include <stdio.h> • void swap(int *x, int *y); /* function declaration */ • int main (){ • int a = 100; /* local variable definition */ • int b = 200; • printf("Before swap, value of a : %dn", a ); • printf("Before swap, value of b : %dn", b ); • /* calling a function to swap the values. • * &a indicates pointer to a i.e. address of variable a and • * &b indicates pointer to b i.e. address of variable b. • */ • swap(&a, &b); • printf("After swap, value of a : %dn", a ); • printf("After swap, value of b : %dn", b ); • return 0; • }/* function definition to swap the values */ • void swap(int *x, int *y) • { • int temp; • temp = *x; /* save the value at address x */ • *x = *y; /* put y into x */ • *y = temp; /* put temp into y */ • Return; • }
  • 13. • /* function definition to swap the values */ • void swap(int x, int y) • { • int temp; • temp = x; /* save the value of x */ • x = y; /* put y into x */ • y = temp; /* put temp into y */ • return; • } • #include <stdio.h> • /* function declaration */ • void swap(int x, int y); • int main () • { • /* local variable definition */ • int a = 100; • int b = 200; • printf("Before swap, value of a : %dn", a ); • printf("Before swap, value of b : %dn", b ); • /* calling a function to swap the values */ • swap(a, b); • printf("After swap, value of a : %dn", a ); • printf("After swap, value of b : %dn", b ); • return 0; • }