Ashish Gupta, Introduction To Computer and Programming@ JUET, Guna 1
Ashish Gupta, Introduction To Computer and Programming@ JUET, Guna 1
Function Overview
Declaration Syntax:
Return_Type Function_Name(argument_list);
Return_Type can be any of data type like char, int, float, double, array, pointer etc.
argument_list can also be any of data type like char, int, float, double, array, pointer
etc.
Return_Type Function_Name(argument_list)
{
stmt;
stmt; Function Body
stmt;
…………
return <expression>;
}
Function_Name(argument_list);
#include<stdio.h>
#include<stdio.h> 775
void convention(int,int,int);
int main(){ int a=5;
convention(a,++a,a++);
return 0;
}
void convention(int p,int q,int r){
printf("%d %d %d",p,q,r);
}
#include<stdio.h> In swap 10 5
void swap(int,int);
In main 5 10
void main(){
int a=5,b=10;
swap(a,b);
printf(“In main %d %d",a,b);
}
void swap(int a,int b){
int temp;
temp =a;
a=b;
b=temp;
printf(“In swap %d %d \n",a,b);
}
Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 10
Ways to Pass Parameters
Call By Reference
In this approach we pass memory address actual variables in function as a parameter.
Hence any modification on parameters inside the function will reflect in the actual
variable.
#include<stdio.h> 10 5
void swap(int *,int *);
int main(){ int a=5,b=10;
swap(&a,&b);
printf("%d %d",a,b);
return 0;
}
void swap(int *a,int *b){
int *temp;
*temp =*a;
*a=*b;
*b=*temp;
}
1st :Take something and Return something (Function with return value and parameters)
Example: printf, scanf , strlen, strcmp etc.
2nd : Take something and Return nothing (Function with no return value but parameters)
Example: delay,
3rd : Take nothing and return something (Function with return value but no parameter)
Example: getch,
4th : Take nothing and return nothing (Function with no return value and no parameter)
Example: clrscr,