
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Pass an array by value in C
Here is an example of passing array by value in C language,
Example
#include <stdio.h> float avg(float a[]) { int i; float avg, sum = 0.0; for (i = 0; i < 6; ++i) { sum += a[i]; } avg = (sum / 6); return avg; } int main() { float avg1, a[] = {63,21,34.4,12.5,3,2.2}; avg1 = avg(a); printf("Average : %f", avg1); return 0; }
Output
Here is the output
Average : 22.683332
In the above program, The actual code of calculating average is present in avg() function. In the for loop, the sum of array elements and average are calculated.
float avg(float a[]) { int i; float avg, sum = 0.0; for (i = 0; i < 6; ++i) { sum += a[i]; } avg = (sum / 6); return avg; }
In the main() function, the values are passed to the array and the function avg() is called.
float avg1, a[] = {63,21,34.4,12.5,3,2.2}; avg1 = avg(a);
- Related Articles
- How to pass an array by reference in C++
- Differences between pass by value and pass by reference in C++
- Pass by reference vs Pass by Value in java
- What is pass by value in C language?
- Which one is better in between pass by value or pass by reference in C++?
- Describe pass by value and pass by reference in JavaScript?
- Is java pass by reference or pass by value?
- What is Pass By Reference and Pass By Value in PHP?
- Is JavaScript a pass-by-reference or pass-by-value language?
- Pass by reference vs value in Python
- How do we pass parameters by value in a C# method?
- How do we pass an array in a method in C#?
- How is Java strictly pass by value?
- Pass an integer by reference in Java
- What is the difference between pass by value and reference parameters in C#?

Advertisements