0% found this document useful (0 votes)
96 views21 pages

Ashish Gupta, Introduction To Computer and Programming@ JUET, Guna 1

The document discusses functions in C programming. It defines a function as a collection of statements that perform a specific task. Functions make programs more modular, understandable, and easier to debug. There are four types of functions: ones that take parameters and return a value, ones that take parameters but return nothing, ones that return a value but take no parameters, and ones that take and return nothing. The document provides examples of each type and discusses function declaration, definition, calling, naming rules, return types, arguments, and recursion.

Uploaded by

Sudhanshu Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
96 views21 pages

Ashish Gupta, Introduction To Computer and Programming@ JUET, Guna 1

The document discusses functions in C programming. It defines a function as a collection of statements that perform a specific task. Functions make programs more modular, understandable, and easier to debug. There are four types of functions: ones that take parameters and return a value, ones that take parameters but return nothing, ones that return a value but take no parameters, and ones that take and return nothing. The document provides examples of each type and discusses function declaration, definition, calling, naming rules, return types, arguments, and recursion.

Uploaded by

Sudhanshu Ranjan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 1

Function Overview

 Function is collection of statements which perform some specific task.


 When any program is very long or same code is repeating many times then we try to
cut the program in different parts (or blocks) so that whole program became more
understandable, easier to debug (error checking) and size of code will be lesser.
 Function is also abbreviated as method or module depends on programming
languages.
 C program can have any number of functions providing that each will have distinct
name
Advantages :
 Program writing becomes easy
 Program becomes easy to understand
 Modification in large program becomes easy
 Modularity comes in program when we use function

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 2


Function Declaration

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.

 Declaration must be before the call of function in main function

Example: int add(int a, int b);

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 3


Function Definition
Definition Syntax:

Return_Type Function_Name(argument_list)
{
stmt;
stmt; Function Body
stmt;
…………
return <expression>;
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 4


Function Call
Call Syntax:

Function_Name(argument_list);

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 5


Sample Example

#include<stdio.h>

int sum (int,int); //function declaration


void main(){
int p;
p=sum(3,4); //function call
printf(“%d”,p);

int sum( int a,int b){


int s; //function body
s=a+b;
return s; //function returning a value
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 6


Function Naming Rules
 Name of function includes alphabets, digit and underscore.
 First character of name of any function must be either alphabets or underscore.
 Name of function cannot be any keyword of c program.
 Name of function cannot be global.
 Name of function cannot be register Pseudo variables
Ex. _AX _AL _AH _SI _ES etc.
 Name of function cannot be exactly same as of name of other function or
identifier within the scope of the function.
 Name of function is case sensitive.
 Only first 32 characters are significant of the function’s name.

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 7


Return Type Rules
 Default return type of function is signed int data type.
 Function can return only one value at a time.
 Return type can be primitive, user defined or derived data type.
 Storage classes allowed with return type are static, extern, typedef i.e. we cannot
use auto and register storage class with the return type of any function.
 Expression in return is optional.
#include<stdio.h> one
void dev(); three
void main(){ two
printf("one\n");
dev();
printf("two\n");
}
void dev(){
printf("three\n");
return;
printf("four\n");
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 8


Argument List Rules
 Arguments or parameters can be primitive, user defined or derived data type.
 Parameters are passed from right hand side in argument list.

#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);
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 9


Ways to Pass Parameters
Call By Value
In this approach we pass copy of actual variables in function as a parameter. Hence any
modification on parameters inside the function will not reflect in the actual variable.

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

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 11


Types of Function

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,

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 12


1st Type Function Example
#include<stdio.h> 6
float area(int); Area of circle is:113.040001
void main()
{
int a;
float b;
scanf(“%d”,&a);
b=area(a);
printf(“Area of circle is:%f”,b);
}
float area(int radius){
float area;
area=3.14*radius*radius;
return area;
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 13


2nd Type Function Example
#include<stdio.h> 6
void area(int); Area of circle is:113.040001
void main()
{
int a;
scanf(“%d”,&a);
area(a);
}
void area(int radius){
float area;
area=3.14*radius*radius;
printf(“Area of circle is:%f”,area);
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 14


3rd Type Function Example
#include<stdio.h> 6
float area(); Area of circle is:113.040001
void main()
{
float b;
b=area();
printf(“Area of circle is:%f”,b);
}
float area(){
float area;
int radius;
scanf(“%d”,&radius);
area=3.14*radius*radius;
return area;
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 15


4th Type Function Example
#include<stdio.h> 6
void area(); Area of circle is:113.040001
void main()
{
area();
}
void area(){
float area;
int radius;
scanf(“%d”,&radius);
area=3.14*radius*radius;
printf(“Area of circle is:%f”,area);
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 16


Nested function
 Call of any function inside any other function
#include<stdio.h>
int max(int a,int b){
if(a>b) 6
return a; 7
else factorial of 7 is 5040
return b; }
int fact(int);
void main(){
int a,b,c;
scanf(“%d%d”,&a,&b);
c=fact(max(a,b));
print(“factorial of %d is %d”,max(a,b),c);
}
int fact(int a){
int fact=1;
while(a>0){
fact=fact*a;
a--;
}
return fact; }

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 17


Recursion
 Calling of same function from its function body is known as function recursion.
 It is alternative of loop.
 Any c program which is possible using loop, it must be possible using function
recursion.
 Recursive function uses stack

Important Steps to make any recursive function:


1. First of all write same program using while loop and function.
2. In that function make all local variable static.
3. Replace loop keyword {i.e. while, for or do..while} by if.
4. The increment or decrement of variable which is used for condition checking, replace
with function call and pass the parameter of that incremented or decremented
variable.

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 18


Factorial Function Example using loop
#include<stdio.h>
int fact(int);
void main(){
int f,num; 6
scanf("%d",&num); factorial of 6 is 720
f=fact(num);
printf(“factorial of %d is %d",num,f);
}
int fact(int a){
int f=1; //make it static
while(a>0){ //replace while by if
f=f*a;
a--; // replace by function call as fact(a-1)
}
return f;
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 19


Factorial Function Example using recursion
#include<stdio.h>
int fact(int);
void main(){
int f,num; 6
scanf("%d",&num); factorial of 6 is 720
f=fact(num);
printf(“factorial of %d is %d",num,f);
}
int fact(int a){
static int f=1;
if(a>0){
f=f*a;
fact(--a);
}
return f;
}

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 20


Summary
 Overview of function
 Function Declaration, definition and function call
 Rules for return type, naming of function and parameters
 Types of function with example
 Nested function
 Recursive function

Ashish Gupta, Introduction to Computer and Programming@ JUET, Guna 21

You might also like