Array to Function
- In this module we will learn how to pass array to a function.
- An entire array can be passed to function as an argument.
- To pass an array to a function, the array must appear by itself, without brackets and subscript as an actual argument within the Function call.
- The corresponding formal argument's are written in the same manner though it must be declared as an array within the formal argument declaration.
- Only passing name means passing the base address of the array.
- Passing just an array name is same as passing &array_name[0].
We Will Understand the passing of array concept by examples.
Q:define a function 'modify' which will take an array and number of elements in an array as an argument and it will just change the all element to array to -5
definition:
#include<stdio.h>
void modify(int arr[],int n); //decleration of function.
void modify(int arr[],int n) //definition of function.
{
for(int i=0;i<i++)
{//modifying each value of array to -5.
arr[i] = -5;
}
}
#include<stdio.h> void modify(int arr[],int n); //decleration of function. void modify(int arr[],int n) //definition of function. { for(int i=0;i<i++) {//modifying each value of array to -5. arr[i] = -5; } }
Using above define Function:
void main()
{
int arr[10] = {1,2,3,4,5};
printf("value before calling modify function:\n");
for(int i=0;i<5;i++)
{
printf("%d\t",arr[i]);
}
printf("\n");
//calling function 'modify()'.
modify(arr,5); //Actual parameters.
printf("values After calling modify function:\n");
for(int i=0;i<5;i++)
{
printf("%d\t",arr[i]);
}
}
void main() { int arr[10] = {1,2,3,4,5}; printf("value before calling modify function:\n"); for(int i=0;i<5;i++) { printf("%d\t",arr[i]); } printf("\n"); //calling function 'modify()'. modify(arr,5); //Actual parameters. printf("values After calling modify function:\n"); for(int i=0;i<5;i++) { printf("%d\t",arr[i]); } }
Ouput:
value before calling modify function:
1 2 3 4 5
values After calling modify function:
-5 -5 -5 -5 -5
1 2 3 4 5
values After calling modify function:
-5 -5 -5 -5 -5
Q:Define function which will take array and number of element in an array as an argument and it should return the sum of array elements.
definition:
int sum_array(int arr[],int n); //declaration
//definition.
int sum_array(int arr[] , int n) //formal parameters.
{
int i,sum = 0;
for(i=0;i<i++)
{
sum = sum + arr[i];
}
return sum;
}
int sum_array(int arr[],int n); //declaration //definition. int sum_array(int arr[] , int n) //formal parameters. { int i,sum = 0; for(i=0;i<i++) { sum = sum + arr[i]; } return sum; }
Using above define Function:
void main()
{
int arr[10] = {1,2,3,4,5};
int result;
//Calling function 'sum_array()'
result = sum_array(arr,5);//formal parameters.
printf("\nSum of array is %d",result);
}
void main() { int arr[10] = {1,2,3,4,5}; int result; //Calling function 'sum_array()' result = sum_array(arr,5);//formal parameters. printf("\nSum of array is %d",result); }
Ouput:
Sum of array is 15
Q:define a function which will take an string as argument and it should perform operation of reversing the string.
definition:
#include<stdio,h>
void reverse_string(char s[])//declaration
void reverse_string(char s[])//definition
{
int i,j,len=0;
char ch;
for(i=0;s[i]!='\0';i++)
{ //calculating the length.
len++;
}
for(j=0;j<i/2;j++)
{
ch = s[j];
s[j] = s[(len-1)-j];
s[(len-1)-j] = ch;
}
}
#include<stdio,h> void reverse_string(char s[])//declaration void reverse_string(char s[])//definition { int i,j,len=0; char ch; for(i=0;s[i]!='\0';i++) { //calculating the length. len++; } for(j=0;j<i/2;j++) { ch = s[j]; s[j] = s[(len-1)-j]; s[(len-1)-j] = ch; } }
Using above define Function:
void main()
{
char s[30] = "Programmers";
//calling function 'reverse_string'
reverse_string(s);//actual parameter.
printf("reverse string is:");
puts(s);
}
void main() { char s[30] = "Programmers"; //calling function 'reverse_string' reverse_string(s);//actual parameter. printf("reverse string is:"); puts(s); }
Ouput:
reverse string is: sremmargorP
Q:define a function which will take an string and a character as an argument and it should perform operation of finding the character 'c' in string 's',return the position of the characters if character is found otherwise return 0.
definition:
int find_char(char s[] ,char c)
{
int i,result;
for(i=0; s[i]!='\0'; i++)
{
if(s[i] == c)
{
result = (i+1);
break;//breaking loop when character is got.
}
else
result = 0;
}
return result;
}
int find_char(char s[] ,char c) { int i,result; for(i=0; s[i]!='\0'; i++) { if(s[i] == c) { result = (i+1); break;//breaking loop when character is got. } else result = 0; } return result; }
Using above define Function:
void main()
{ int Position;
char s[30] = "Programmers are great";
//calling function 'find_char'
Position = find_char(s,'o');//actual parameters.
if(Position == 0)
printf("\nCharacter Not Found");
else
printf("\nCharacter was found at %d position.",Position);
}
void main() { int Position; char s[30] = "Programmers are great"; //calling function 'find_char' Position = find_char(s,'o');//actual parameters. if(Position == 0) printf("\nCharacter Not Found"); else printf("\nCharacter was found at %d position.",Position); }
Ouput:
Character was found at 3 position.
Further Concepts:
Further Topics:
- What are Pointers?
- how to declare pointers?
- how to initialize a pointers?
- How to access the pointer variables?
- Function of '*' and '&' operators
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- How to declare Structure Pointer?
- Working with structure Pointer
- How to Create Structure Variable Dynamically ?
- Creating structure variable through malloc() and calloc()
People Also Searched:
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() functions
- Strings In C
- Concept of strings
0 Comments
Post a Comment