Thanks to visit codestin.com
Credit goes to www.slideshare.net

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;
• }

Function (rule in programming)

  • 1.
  • 2.
    Function • A largeC 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
  • 3.
  • 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 Cfunctions 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 CALLC 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.
    • /* functiondefinition 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; • }