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

Understanding Functions in C Programming

The document explains the concept of functions in programming, detailing their benefits such as reusability and modularity. It covers the types of functions (library and user-defined), the steps to create and use user-defined functions, and provides examples of function declaration, definition, and calling. Additionally, it discusses recursion, passing arrays to functions, and the difference between call by value and call by reference.

Uploaded by

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

Understanding Functions in C Programming

The document explains the concept of functions in programming, detailing their benefits such as reusability and modularity. It covers the types of functions (library and user-defined), the steps to create and use user-defined functions, and provides examples of function declaration, definition, and calling. Additionally, it discusses recursion, passing arrays to functions, and the difference between call by value and call by reference.

Uploaded by

satish mane
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

GIRI’S TECH HUB PVT.

LTD – PUNE - 9175444433, 9049361265

Function: function is a block of statement which is used for writing some logics.
Q. why use function or what is benefit of function?
_______________________________________________________________________________
1. Reusability: reusability means we can define function only once and we can reuse multiple times.

2. Modularity: we can divide the large code in to sub code and integrate them internally called as
modularity.

There are two types of function


1) Library function: those function already present in c language called as library function.
like as printf, scanf, etc
2) User Defined function: those function define by user for its own use called as user defined functions.

if we want to work with a user defined functions we have three steps.

1. Declaration: declaration means we specify function name, its data type and its argument type called
as declaration.

Syntax: return type functionname(datatype variablename);


Example: void add(int ,int);
if we think about above declaration we have function name as add() which contain two parameter of
type integer and it has return type name as void
Note: when we have void as return type then we cannot return any value from a function.

2. Definition: definition means we write actual logic in function called as definition or called function

Syntax:
return type functionname(data type variablename)
{ write here your logics
}
Example:
void add(int x,int y)
{ printf(“Addition is %d\n”,x+y);
}
Important points related with a function definition
A. definition must be outside of main or any other function
B. definition must be match with a declaration
C. in definition data type and variable must be present if function contains parameter
D. if function return type is void then cannot use return keyword in function definition

Visit: [Link] Download : GIRI’S TECH HUB -App Page 1


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

E: if function return type is not void then we can return value from a function
F: Function definition execute when function call

3. Calling: when we want to use function then we need to call it means we want reuse logic from a
function we need to call it.
Syntax: functionname(variablename)
Example: add(100,200);

Important points related with a function calling


1. Function name must be same with a declaration
2. Not need to specify data type in function calling just we need to specify variables or values.
3. If function definition returns some result then we need to use variable at function calling point
as per return type.

How to reuse function logics


Steps.
1. Define function
#include<stdio.h>
void add(int x,int y){
printf("Addition is %d\n",x+y);
}
2. Create own header file
Save the file using .h extension
Example: myadd.h

3. Include own header file where we want to use function


#include "myadd.h"
int main(){

Visit: [Link] Download : GIRI’S TECH HUB -App Page 2


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

int a,b;
printf("\nEnter two values\n");
scanf("%d %d",&a,&b);

return 0;
}
4. Call the function

#include "myadd.h"
int main(){
int a,b;
printf("\nEnter two values\n");
scanf("%d %d",&a,&b);
add(a,b); //Function call here and jump in myadd.h definition
return 0;
}

Example: WAP to create function name as power with two parameters int base and index and
calculate its power

Your Task: Create own library name as pow.h and define power function in pow.h and call it in main
function where we use it.

pow.h
#include<stdio.h>
void power(int base,int index) //defination
{
int p=1;
for(int i=1; i<=index;i++){
p = p * base;

Visit: [Link] Download : GIRI’S TECH HUB -App Page 3


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

}
printf("\nPower is %d\n",p);
}

[Link]
#include "pow.h"
int main(){
int b,i;
printf("\nenter base and index\n");
scanf("%d %d",&b,&i);
power(b,i);//calling

return 0;
}

How to return value from a function


if we want to return value from a function then your function return type should not void and mention
the data type with function which kind value we want to return

Example: int getSquare(int x)


{ return x*x;
}
if we think about this code we have function name as getSquare() which return type is integer means we
can return integer value from this function definition to function calling and for return value we need to
use return keyword in function definition
So when we use getSquare() then we must be use variable name at left hand side of function calling.

Visit: [Link] Download : GIRI’S TECH HUB -App Page 4


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

Example:
#include<stdio.h>
int main(){
int getSquare(int);//declaration
int no;
printf("\nEnter number\n");
scanf("%d",&no);
int result=getSquare(no);
printf("\nSquare is %d\n",result);
return 0;
}
int getSquare(int x){
return x*x;
}

Example: WAP create function name as int getSum(int x) and input number from keyboard and
calculate sum of all its digit and return it.

sum.h
int getSum(int no){
int s=0;
while(no!=0){
int rem = no % 10;
no = no /10;

Visit: [Link] Download : GIRI’S TECH HUB -App Page 5


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

s=s+rem;
}
return s;
}
[Link]
#include "sum.h"
#include <stdio.h>
int main(){
int no;
printf("\nEnter number\n");
scanf("%d",&no);
int result= getSum(no);
printf("\nResult is %d\n",result);
return 0;
}

Example: we want to create function name as int getPer(int ,int,int,int,int,int) this function
accept six parameter of type integer and calculate its sum and return it.

#include<stdio.h>
int getPer(int s1,int s2,int s3,int s4,int s5,int s6){
int per=s1+s2+s3+s4+s5+s6;
return (per/6);
}
int main(){
int sub1,sub2,sub3,sub4,sub5,sub6,result;
printf("\nEnter six subject marks\n");
scanf("%d %d %d %d %d %d",&sub1,&sub2,&sub3,&sub4,&sub5,&sub6);
result=getPer(sub1,sub2,sub3,sub4,sub5,sub6);//calling
printf("\nPercentage is %d\n",result);

return 0;
}
The limitation of above code is we pass 6 parameter of same type in function definition
but it is not good approach when we have multiple parameter of same type then we can pass array as
parameter to the function.

How to pass array as parameter in function


_______________________________________________________________________________

Visit: [Link] Download : GIRI’S TECH HUB -App Page 6


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

If we want to pass array as parameter in function then we need to pass base address of array at function
calling point.

Q. WAP to create function name as getCube (int x) in cube.h header file and create one file name as
[Link] and input number and calculate cube and return it.

Example: WAP to create function name as rev(int no) and input number from keyboard and reverse the
number and return it.

Visit: [Link] Download : GIRI’S TECH HUB -App Page 7


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

Example: WAP to create function name as int getMax(int a[]) this function can find the max value from
array and return at function calling point.

max.h
int getMax(int a[]){
int m=a[0];
for(int i=0; i<5;i++){
if(a[i]>m)
{
m=a[i];
}

}
return m;
}
[Link]
#include<stdio.h>
#include "max.h"
int main(){
int a[5],i;
printf("\nEnter five values in array\n");
for(i=0; i<5;i++){
scanf("%d",&a[i]);
}
int result=getMax(a);

Visit: [Link] Download : GIRI’S TECH HUB -App Page 8


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

printf("\nMax value is %d\n",result);

return 0;
}
Example: WAP to create function name as int search(int a[],int value): this function can accept array as
parameter as well as accept one more value and search value in array and return its index and if
element not found return -1
search.h
int search(int a[],int key){
int flag=0,i;
for(i=0;i<5;i++){
if(a[i]==key){
flag=1;
break;
}
}
if(flag){
return i;
}
else{
return -1;
}
}
[Link]
#include <stdio.h>
#include "search.h"
int main(){
int a[5],i,skey;
printf("\nEnter five values in array\n");
for(i=0;i<5;i++){
scanf("%d",&a[i]);
}
printf("\nEnter search key\n");
scanf("%d",&skey);
int result= search(a,skey);
if(result!=-1){
printf("Value found %d",a[result]);
}

Visit: [Link] Download : GIRI’S TECH HUB -App Page 9


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

else{ printf("Value not found");


}
return 0;
}
Example: WAP to create function name as reverseArray (int a []) this function is used for accept array as
parameter and reverse it

Function Recursion
Function Recursion
_____________________________________________________________________________
Function recursion means a function call itself again and again called as function recursion means we
can say in the case of function recursion we call same function from its own definition
Syntax:
return type functionname(datatype variablename){
-----------------------------
----------------------------
Functionname (variablename) //recursion vall
}

Example: we want to print the good morning message five times using a recursion

Visit: [Link] Download : GIRI’S TECH HUB -App Page 10


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

Source code
#include<stdio.h>
void show(int no){
if(no!=0){
printf("good morning\n");
show(--no);
}
else{
printf("END\n");
}
}
int main(){
int n;
printf("\nEnter number\n");
scanf("%d",&n);
show(n);
return 0;
}

Example: WAP to input number and print its table using recursion.

Visit: [Link] Download : GIRI’S TECH HUB -App Page 11


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

Source code
#include<stdio.h>
int count=0;
void table(int x){
if(count<10){
++count;
printf("%d\n",count*x);
table(x);
}
}
int main(){
int no;
printf("\nEnter number\n");
scanf("%d",&no);
printf("\n============================\n");
table(no);
return 0;
}

Example: WAP to input two values consider first as base and second as index and calculate its power
using recursion.

Visit: [Link] Download : GIRI’S TECH HUB -App Page 12


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

Example: WAP to input number and reverse it using a recursion.

Source code
#include<stdio.h>
int rev(int no,int r){
if(no!=0){
int rem=no%10;
no = no /10;
r=r*10+rem;
rev(no,r);
}
else{
return r;

Visit: [Link] Download : GIRI’S TECH HUB -App Page 13


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

}
}
int main(){
int no;
printf("Enter number\n");
scanf("%d",&no);
int result = rev(no,0);
printf("\nReverse is %d\n",result);
return 0;
}

Example: WAP to input number and check number is perfect or not using a recursion

Example: WAP to create function name as getMax(int a[]) this function will accept integer array as
parameter and find max value and return it using a recursion

Example: WAP to create function name as int search(int a[],int value,int index,int len): this function
accept array as parameter and one key value and search in array and return its index if value not found
return -1 using a recursion

#include<stdio.h>
int search(int a[],int len,int index,int skey){
if(index<len){
if(a[index]==skey){
return index;
}
else{
Visit: [Link] Download : GIRI’S TECH HUB -App Page 14
GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

return search(a,len,++index,skey);
}
}
else{
return -1;
}
}
int main(){
int a[5],len,index,skey,i;
printf("Enter values in array\n");
len=sizeof(a)/sizeof(a[0]);
for(i=0; i<len; i++){
scanf("%d",&a[i]);
}
printf("Enter search key\n");
scanf("%d",&skey);
index=search(a,len,0,skey);
if(index!=-1){
printf("Search value is %d\n",a[index]);
}
else{
printf("\nValue not found\n");
}

return 0;
}

Example: WAP to create function name as void sort(int a[]) and perform sorting on array by using
recursion
#include<stdio.h>
int index=0,len;
void sort(int a[]){
if(index<len) //check condition for outer loop
{
for(int j=(index+1);j<len;j++){
if(a[index]>a[j]){
int temp=a[index];
a[index]=a[j];
a[j]=temp;

Visit: [Link] Download : GIRI’S TECH HUB -App Page 15


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

}
}
++index;
sort(a); //manage outer loop
}

}
int main(){
int a[5],i;
printf("\nEnter five values in array\n");
len=sizeof(a)/sizeof(a[0]);
for(i=0; i<len;i++){
scanf("%d",&a[i]);
}
printf("\nBefore Sorting\n");
for(i=0; i<len;i++){
printf("%d\t",a[i]);
}
sort(a);
printf("\nAfter Sorting\n");
for(i=0; i<len;i++){
printf("%d\t",a[i]);
}
return 0;
}

Call by Value and Call by Reference


_________________________________________________________________________
Call by Value means we call function by passing normal value or variables and call by reference means
we call function by passing address of variable
So if we think about call by value we pass duplicated values from function calling to function definition
and if we think about call by reference then we pass actual values from a function calling to function
definition.
In the case of call by value if we perform change in function definition it will never reflect on function
calling and in the case of call by reference if we perform change in function definition it will reflect on
function calling.

Example of Call by value


__________________________________________________________________________

Visit: [Link] Download : GIRI’S TECH HUB -App Page 16


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

if we think about above diagram we have function name as void swap(int x,int y) and we call it from
main function swap(a,b) means here we pass normal values to function definition means we not send
address of the variable so we can say it is call by value
means we have variables in swap function x,y and z and we have variable in main function a and b these
are the local variable every function has separate memory block so we when we call swap function then
variable a pass its duplicate to variable x and variable b pass its duplicated value to variable y so when
we perform swapping with variable x and y then variable a and b not change its original value means
swapping not perform at function calling point so it is call by value.

Source code using call by value


___________________________________________________________________________
#include<stdio.h>
void swap(int x,int y){
int z=x;
x=y;
y=z;
printf("\n\tX=%d\tY=%d\n",x,y);
}
int main(){
int a,b;
printf("\nEnter two values\n");
scanf("%d %d",&a,&b);
swap(a,b);
printf("\n\tA=%d\tB=%d\n",a,b);
return 0;

Visit: [Link] Download : GIRI’S TECH HUB -App Page 17


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

Example of Call by Reference


_______________________________________________________________________________
in the case of call by reference we use pointer variable in a function definition and pass address from
function calling means we send original memory access of calling variable to definition pointer so in the
case of call by reference if we perform any change in function definition it will reflect on function calling
shown in following diagram.

Example of call by reference

Visit: [Link] Download : GIRI’S TECH HUB -App Page 18


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

How to return array from a function


______________________________________________________________________________
If we want to return array from a function in c language we required to set pointer as return type of
function and return base address of array.

Example of Call by Reference


________________________________________________________________________

Global Variable and Local Variable


_________________________________________________________________________________
Global variable means a variable can access anywhere in program means those variable declared before
main function called as global variable and default value of global variable is 0 and Local variable means
variable cannot access outside of function or block called as local variable

Visit: [Link] Download : GIRI’S TECH HUB -App Page 19


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

means if we declare variable within a function called as local variable and local variable cannot use
outside of his own function and if we try to use then compiler will generate compile time error to us

#include<stdio.h>
int m=100;//global variable
int main(){
void show();
int a=200; //local variable
printf("\n\t\tM = %d\n",m);
printf("\n\t\tA = %d\n",a);
show();
return 0;
}
void show(){
printf("\n\t\tM In Show =%d\n",m);
printf("\n\t\tA In Show %d\n",a); //generate compile time error for a
//because it is declared in main function but we try to use it
//in show function and it is not possible
//because local variable cannot access outside of his own function.
}
if we want to access local variable of one function to another function then we need to pass parameter
from a function calling to function definition means as per our example if we want to pass variable a
value to function show() from main function then we need to call show() function in main function and
pass parameter from main function to show() function means parameter help us to share data between
two different function with each other.

Show in following code.


#include<stdio.h>
int m=100;//global variable
int main(){
void show(int);
int a=200; //local variable
printf("\n\t\tM = %d\n",m);
printf("\n\t\tA = %d\n",a);
show(a);
return 0;
}
void show(int a){
printf("\n\t\tM In Show =%d\n",m);

Visit: [Link] Download : GIRI’S TECH HUB -App Page 20


GIRI’S TECH HUB [Link] – PUNE - 9175444433, 9049361265

printf("\n\t\tA In Show %d\n",a);


}

Visit: [Link] Download : GIRI’S TECH HUB -App Page 21

You might also like