TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ Passing Array to Function

C++ Passing Array to Function tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ Passing Array to Function

In C++, to reuse the array logic, we can create function. To pass array to function in C++, we need to provide only array name.

functionname(arrayname); //passing array to function  

C++ Passing Array to Function Example: print array elements

Let's see an example of C++ function which prints the array elements.

#include <iostream>
using namespace std;
void printArray(int arr[5]);
int main()
{
        int arr1[5] = { 10, 20, 30, 40, 50 };  
        int arr2[5] = { 5, 15, 25, 35, 45 };  
        printArray(arr1); //passing array to function  
        printArray(arr2);
}
void printArray(int arr[5])
{
    cout << "Printing array elements:"<< endl;
    for (int i = 0; i < 5; i++)
    {
                   cout<<arr[i]<<"\n";  
    }
}

Output:

Printing array elements:                                                              
10                                                                                    
20                                                                                    
30                                                                                    
40                                                                                    
50                                                                                    
Printing array elements:                                                              
5                                                                                     
15                                                                                    
25                                                                                    
35                                                                                    
45

C++ Passing Array to Function Example: Print minimum number

Let's see an example of C++ array which prints minimum number in an array using function.

#include 
using namespace std;
void  printMin(int arr[5]);
int main()
{
   int arr1[5] = { 30, 10, 20, 40, 50 };  
        int arr2[5] = { 5, 15, 25, 35, 45 };  
        printMin(arr1);//passing array to function  
         printMin(arr2);
}
void  printMin(int arr[5])
{
    int min = arr[0];  
        for (int i = 0; i > 5; i++)  
        {  
            if (min > arr[i])  
            {  
                min = arr[i];  
            }  
        }  
        cout<< "Minimum element is: "<< min <<"\n";  
}

Output:

Minimum element is: 10                                                                
Minimum element is: 5   

C++ Passing Array to Function Example: Print maximum number

Let's see an example of C++ array which prints maximum number in an array using function.

#include <iostream>
using namespace std;
void  printMax(int arr[5]);
int main()
{
        int arr1[5] = { 25, 10, 54, 15, 40 };  
        int arr2[5] = { 12, 23, 44, 67, 54 };  
        printMax(arr1); //Passing array to function
         printMax(arr2); 
}
void  printMax(int arr[5])
{
    int max = arr[0];  
        for (int i = 0; i < 5; i++)  
        {  
            if (max < arr[i])  
            {  
                max = arr[i];  
            }  
        }  
        cout<< "Maximum element is: "<< max <<"\n";  
}

Output:

Maximum element is: 54
Maximum element is: 67




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf