Programming in C
Module IV
Rajagiri School of Engineering &
Technology
• Functions
• Elements of User Defined Function
• Methods of calling a Function
Contents
Module 4 2
Functions
• You can divide up your code into separate functions. Logically
the division is such that each function performs a specific task.
• A function can also be referred as a method or a sub-routine or
a procedure.
Functions
Module 4 4
A function is a group of statements that together perform a task.
1. Function Prototype (function declaration)
2. Function Definition
3. Function Call
Elements of User Defined Function
Module 4 5
• A function declaration tells the compiler about a function
name and how to call the function.
• Syntax:
• Example:
void xyz(int, char,float);
1.
2.
1. Function Declaration
Module 4 6
returntype function_name (Parameter List);
• Contains all the statements to be executed.
2.Function Definition
Module 4 7
returntype function_name (parameter list)
{
//body of the function
}
Example
Module 4 8
• When a program calls a function, the program control
is transferred to the called function.
3. Function Call
Module 4 9
function_name(arguments list);
Module 4 10
int max(int num1,int num2);
void main()
{
int a=100,b=200,ret;
ret=max(a,b);
printf(“the max value is %d”,ret);
}
int max(int num1,int num2)
{
int result;
If(num1>num2)
result=num1;
Else result=num2;
return result;
}
Example
Module 4 11
Example
Module 4 12
1. Function with no arguments and no return value
2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and a return value
Categories of User-defined Functions
Module 4 13
1. Function with no arguments and no return value
Module 4 14
Function Call
Module 4 15
2. Function with no arguments and a return value
3. Function with argument and no return value
Module 4 16
4. Function with argument and return value
Module 4 17
• Call by Value
• Call by Reference
Methods of calling a Function
Module 4 18
• 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.
Call by value method
Module 4 19
#include<stdio.h>
// function declaration
void swap(int a, int b);
int main()
{
int m = 22, n = 44;
printf(" values before swap m = %d n and n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" nvalues after swap m = %dn and n = %d", a, b);
}
Example
Module 4 20
• Pass Individual Array Elements
• Pass Arrays to Functions
– Function Prototype: void pass(int []);
– Function call: pass(a);
– Function definition:
• void pass(int b[])
– {for(int i=0;i<3;i++)
– printf("%dn",b[i]);}
Pass arrays to a function in C
Module 4 21
//pass second and third elements to display()
display(ageArray[1], ageArray[2]);
Function Definition
void display(int age1, int age2)
{ // code }
22
Module 4

functions (1).pptx

  • 1.
    Programming in C ModuleIV Rajagiri School of Engineering & Technology
  • 2.
    • Functions • Elementsof User Defined Function • Methods of calling a Function Contents Module 4 2
  • 3.
  • 4.
    • You candivide up your code into separate functions. Logically the division is such that each function performs a specific task. • A function can also be referred as a method or a sub-routine or a procedure. Functions Module 4 4 A function is a group of statements that together perform a task.
  • 5.
    1. Function Prototype(function declaration) 2. Function Definition 3. Function Call Elements of User Defined Function Module 4 5
  • 6.
    • A functiondeclaration tells the compiler about a function name and how to call the function. • Syntax: • Example: void xyz(int, char,float); 1. 2. 1. Function Declaration Module 4 6 returntype function_name (Parameter List);
  • 7.
    • Contains allthe statements to be executed. 2.Function Definition Module 4 7 returntype function_name (parameter list) { //body of the function }
  • 8.
  • 9.
    • When aprogram calls a function, the program control is transferred to the called function. 3. Function Call Module 4 9 function_name(arguments list);
  • 10.
  • 11.
    int max(int num1,intnum2); void main() { int a=100,b=200,ret; ret=max(a,b); printf(“the max value is %d”,ret); } int max(int num1,int num2) { int result; If(num1>num2) result=num1; Else result=num2; return result; } Example Module 4 11
  • 12.
  • 13.
    1. Function withno arguments and no return value 2. Function with no arguments and a return value 3. Function with arguments and no return value 4. Function with arguments and a return value Categories of User-defined Functions Module 4 13
  • 14.
    1. Function withno arguments and no return value Module 4 14 Function Call
  • 15.
    Module 4 15 2.Function with no arguments and a return value
  • 16.
    3. Function withargument and no return value Module 4 16
  • 17.
    4. Function withargument and return value Module 4 17
  • 18.
    • Call byValue • Call by Reference Methods of calling a Function Module 4 18
  • 19.
    • In callby 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. Call by value method Module 4 19
  • 20.
    #include<stdio.h> // function declaration voidswap(int a, int b); int main() { int m = 22, n = 44; printf(" values before swap m = %d n and n = %d", m, n); swap(m, n); } void swap(int a, int b) { int tmp; tmp = a; a = b; b = tmp; printf(" nvalues after swap m = %dn and n = %d", a, b); } Example Module 4 20
  • 21.
    • Pass IndividualArray Elements • Pass Arrays to Functions – Function Prototype: void pass(int []); – Function call: pass(a); – Function definition: • void pass(int b[]) – {for(int i=0;i<3;i++) – printf("%dn",b[i]);} Pass arrays to a function in C Module 4 21 //pass second and third elements to display() display(ageArray[1], ageArray[2]); Function Definition void display(int age1, int age2) { // code }
  • 22.