CSE 191 – COMPUTER PROGRAMMING
Najia Manjur
Lecturer
Department of Computer Science & Engineering (CSE)
Military Institute of Science & Technology (MIST)
Mirpur, Dhaka – 1216, Bangladesh
Introduction to Function
 A large program can be divided into many subprograms. These
subprograms can be called Functions.
 They allow us to reuse code instead of rewriting it.
 Functions allow us to test small parts of our program in isolation from the
rest.
 Save time and space of the code.
 Library Functions : printf( ), scanf( ), gets( ), pow( ) etc.
 User Defined Functions : main( )
2
Functions
 Function Definition.
 Function Call
 Function Declaration.
3
#include<stdio.h>
long cube(long x); //function prototype
long input,answer;
int main( )
{ printf(“Enter values: “);
scanf(“%d”,&input);
answer= cube(input); //function call
printf(“The cub of %ld is %ldn”,input,answer);
return 0; }
long cube(long x) //function definition
{ long x_cubed;
x_cubed = x*x*x;
return x_cubed; }
long cube(long x) function_return_type function_name (parameter
list)
{ function header
long x_cubed; //local variable
x_cubed = x*x*x; //executable statements function body
return x_cubed; //return statement
}
Function Definition
4
int main( )
{
printf(“Enter values: ”);
scanf(“%d”,&input);
answer= cube(input); //function call
printf(“The cub of %ld is %ldn”,input,answer);
return 0; }
Function Call
5
function_return_type function_name (parameter list);
long cube(long x);
Function Prototype
6
 parameter list must be separated by commas.
 parameter names do not need to be same in prototype declaration
and function definition.
 parameters must match in type, number and order.
 return type must be void when the function does not return a value.
 if declared types do not match with types in function definition,
compiler will produce an error.
Important
7
Category of Functions
 no arguments and no return values.
 Called function does not receive any data from calling function.
 Calling function does not receive any data from called function.
 arguments but no return value.
 Calling function sends values to called function using function call with proper
arguments
 Calling function does not receive any data from called function.
 argument and a return value.
 Called function receives data from calling function.
 Calling function receives a value from called function.
 no argument but a return value.
 Calling function sends no data to called function.
 Calling function receives a value from called function.
8
No Arguments and No Return Value
void sum_series (void) ; //declaration
int main( )
{ sum_series( ); // function call
return 0 ; }
void sum_series(void) // definition
{ int sum=0,i=1, n;
scanf(“%d”,&n);
for(i=1 ; i<=n ; i++)
sum+=i;
printf(“%d”,sum); }
9
Step1
Step2
Arguments but No Return Value
void multi(float p , float q); //declaration
int main( )
{ multi(2 .3 , 3 .4); // function call
return 0 ; }
void multi(float a , float b) // definition
{ float ans=0;
ans= a*b;
printf(“%.2f”,ans); }
10
Arguments and A Return Value
float average(float a, int b) ; //declaration
int main( )
{ float a=0;
a=average(2 . 5 , 5); // function call
printf(“%f”,a);
return 0 ; }
float average(float x, int y) // definition
{ float avg=0;
avg=(float) (2.5*5)/2;
return avg;
}
11
No Arguments but A Return Value
int get_number (void) ; //declaration
int main( )
{ int m=get_number( ); // function call
printf(“%d”,m);
return 0 ; }
int get_number(void) // definition
{ int number ;
scanf(“%d”,&number);
return number; }
12
Nested Function ( without declaration)
int isequal(int p, int q)
{ if(p!=q) return 1;
else return 0; }
void difference(int y, int z)
{ if(isequal(y , z))
printf(“%d”,y-z);
else printf(“No difference”); }
int main( )
{ int a, b; scanf(“%d %d”,&a, &b);
difference(a,b);
return 0; }
13
1
3
2
4
Nested Function ( with declaration)
void difference(int y, int z);
int isequal(int p, int q);
void difference(int y, int z)
{ if(isequal(y , z))
printf(“%d”,y-z);
else printf(“No difference”); }
int isequal(int p, int q)
{ if(p!=q) return 1;
else return 0; }
int main( )
{ int a, b; scanf(“%d %d”,&a, &b);
difference(a,b);
return 0; }
14
Array as an argument
void sort(int m, int x[ ]); //declaration
main( )
{
sort(5, marks); //function call
//marks is the array name sent from main
function to sort() function
}
void sort(int m, int x[ ]) // definition
{ //code }
15
Array as an argument
when a function changes the values of the elements of
an array, these changes will be made to the original
array that was passed to the function.
when an entire array is passed as an argument, the
contents of the array are not copied to the formal
parameter array, but information about the address of
array elements are passed on to the function.
so, any changes introduced to the array reflects on the
original array in the calling function.
16
End of Slides

Functions

  • 1.
    CSE 191 –COMPUTER PROGRAMMING Najia Manjur Lecturer Department of Computer Science & Engineering (CSE) Military Institute of Science & Technology (MIST) Mirpur, Dhaka – 1216, Bangladesh
  • 2.
    Introduction to Function A large program can be divided into many subprograms. These subprograms can be called Functions.  They allow us to reuse code instead of rewriting it.  Functions allow us to test small parts of our program in isolation from the rest.  Save time and space of the code.  Library Functions : printf( ), scanf( ), gets( ), pow( ) etc.  User Defined Functions : main( ) 2
  • 3.
    Functions  Function Definition. Function Call  Function Declaration. 3 #include<stdio.h> long cube(long x); //function prototype long input,answer; int main( ) { printf(“Enter values: “); scanf(“%d”,&input); answer= cube(input); //function call printf(“The cub of %ld is %ldn”,input,answer); return 0; } long cube(long x) //function definition { long x_cubed; x_cubed = x*x*x; return x_cubed; }
  • 4.
    long cube(long x)function_return_type function_name (parameter list) { function header long x_cubed; //local variable x_cubed = x*x*x; //executable statements function body return x_cubed; //return statement } Function Definition 4
  • 5.
    int main( ) { printf(“Entervalues: ”); scanf(“%d”,&input); answer= cube(input); //function call printf(“The cub of %ld is %ldn”,input,answer); return 0; } Function Call 5
  • 6.
    function_return_type function_name (parameterlist); long cube(long x); Function Prototype 6
  • 7.
     parameter listmust be separated by commas.  parameter names do not need to be same in prototype declaration and function definition.  parameters must match in type, number and order.  return type must be void when the function does not return a value.  if declared types do not match with types in function definition, compiler will produce an error. Important 7
  • 8.
    Category of Functions no arguments and no return values.  Called function does not receive any data from calling function.  Calling function does not receive any data from called function.  arguments but no return value.  Calling function sends values to called function using function call with proper arguments  Calling function does not receive any data from called function.  argument and a return value.  Called function receives data from calling function.  Calling function receives a value from called function.  no argument but a return value.  Calling function sends no data to called function.  Calling function receives a value from called function. 8
  • 9.
    No Arguments andNo Return Value void sum_series (void) ; //declaration int main( ) { sum_series( ); // function call return 0 ; } void sum_series(void) // definition { int sum=0,i=1, n; scanf(“%d”,&n); for(i=1 ; i<=n ; i++) sum+=i; printf(“%d”,sum); } 9 Step1 Step2
  • 10.
    Arguments but NoReturn Value void multi(float p , float q); //declaration int main( ) { multi(2 .3 , 3 .4); // function call return 0 ; } void multi(float a , float b) // definition { float ans=0; ans= a*b; printf(“%.2f”,ans); } 10
  • 11.
    Arguments and AReturn Value float average(float a, int b) ; //declaration int main( ) { float a=0; a=average(2 . 5 , 5); // function call printf(“%f”,a); return 0 ; } float average(float x, int y) // definition { float avg=0; avg=(float) (2.5*5)/2; return avg; } 11
  • 12.
    No Arguments butA Return Value int get_number (void) ; //declaration int main( ) { int m=get_number( ); // function call printf(“%d”,m); return 0 ; } int get_number(void) // definition { int number ; scanf(“%d”,&number); return number; } 12
  • 13.
    Nested Function (without declaration) int isequal(int p, int q) { if(p!=q) return 1; else return 0; } void difference(int y, int z) { if(isequal(y , z)) printf(“%d”,y-z); else printf(“No difference”); } int main( ) { int a, b; scanf(“%d %d”,&a, &b); difference(a,b); return 0; } 13 1 3 2 4
  • 14.
    Nested Function (with declaration) void difference(int y, int z); int isequal(int p, int q); void difference(int y, int z) { if(isequal(y , z)) printf(“%d”,y-z); else printf(“No difference”); } int isequal(int p, int q) { if(p!=q) return 1; else return 0; } int main( ) { int a, b; scanf(“%d %d”,&a, &b); difference(a,b); return 0; } 14
  • 15.
    Array as anargument void sort(int m, int x[ ]); //declaration main( ) { sort(5, marks); //function call //marks is the array name sent from main function to sort() function } void sort(int m, int x[ ]) // definition { //code } 15
  • 16.
    Array as anargument when a function changes the values of the elements of an array, these changes will be made to the original array that was passed to the function. when an entire array is passed as an argument, the contents of the array are not copied to the formal parameter array, but information about the address of array elements are passed on to the function. so, any changes introduced to the array reflects on the original array in the calling function. 16
  • 17.