0% found this document useful (0 votes)
5 views11 pages

Week 7 Notes

The document provides an overview of how to create and call functions in C programming, including examples of functions with various characteristics such as returning values, accepting arguments, and having void return types. It also explains the use of arrays, including how to declare, access, modify, and loop through array elements, along with examples for input/output and calculating averages. Overall, it serves as a guide for understanding functions and arrays in C.

Uploaded by

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

Week 7 Notes

The document provides an overview of how to create and call functions in C programming, including examples of functions with various characteristics such as returning values, accepting arguments, and having void return types. It also explains the use of arrays, including how to declare, access, modify, and loop through array elements, along with examples for input/output and calculating averages. Overall, it serves as a guide for understanding functions and arrays in C.

Uploaded by

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

How to call a function in C?

Consider the following C program

Example1: Creating a user defined function addition()


#include <stdio.h>
int addition(int num1, int num2)
{
int sum;
/* Arguments are used here*/
sum = num1+num2;

/* Function return type is integer so we are returning


* an integer value, the sum of the passed numbers.
*/
return sum;
}

int main()
{
int var1, var2;
printf("Enter number 1: ");
scanf("%d",&var1);
printf("Enter number 2: ");
scanf("%d",&var2);

/* Calling the function here, the function return type


* is integer so we need an integer variable to hold the
* returned value of this function.
*/
int res = addition(var1, var2);
printf ("Output: %d", res);

return 0;
}
Output:

Enter number 1: 100


Enter number 2: 120
Output: 220
Example2: Creating a void user defined function that doesn’t
return anything
#include <stdio.h>
/* function return type is void and it doesn't have parameters*/
void introduction()
{
printf("Hi\n");
printf("My name is Chaitanya\n");
printf("How are you?");
/* There is no return statement inside this function, since
its
* return type is void
*/
}

int main()
{
/*calling function*/
introduction();
return 0;
}
Output:

Hi
My name is Chaitanya
How are you?

C Programming Functions Without Arguments and Return Value

A function in C programming may not accept an argument and return a value. Here’s an
example of such a function.

#include<stdio.h>

void main (){

printf("Welcome to ");

printName();

}
void printName(){

printf("Simplilearn");

Output:

C programming Functions With No Arguments But has a Return Value

A function can return a value without accepting any arguments. Here’s an example of
calculating and returning the area of a rectangle without taking any argument.

#include<stdio.h>

void main(){

int area = rect_Area();

printf("The area of the Rectangle is: %d\n",area);

int rect_Area(){

int len, wid;

printf("Enter the length of the rectangle: ");

scanf("%d",&len);
printf("Enter the width of the rectangle: ");

scanf("%d",&wid);

return len * wid;

Output:

C programming Functions With Arguments But No Return Value

C functions may accept arguments but not provide any return value. Given below is an
example of such a function.

#include<stdio.h>

void main(){

int x,y;

printf("Enter the two numbers to add:");

scanf("%d %d",&x,&y);

add(x,y);

// Accepting arguments with void return type


void add(int x, int y){

printf("The sum of the numbers is %d",x+y);

Output:

C Programming Functions That Accept Arguments and Give a Return Value

Most C functions will accept arguments and provide a return value. The following
program demonstrates a function in C programming that takes arguments and returns a
value.

#include<stdio.h>

void main(){

int x,y,res;

printf("Enter the two numbers to add:");

scanf("%d %d",&x,&y);

res = add(x,y);

printf("The sum of the numbers is %d",res);

int add(int x, int y){


return x+y;

Output:

Arrays
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.

To create an array, define the data type (like int) and specify the name of the
array followed by square brackets [].

To insert values to it, use a comma-separated list inside curly braces, and make
sure all values are of the same data type:

int myNumbers[] = {25, 50, 75, 100};

We have now created a variable that holds an array of four integers.

Access the Elements of an Array


To access an array element, refer to its index number.

Array indexes start with 0: [0] is the first element. [1] is the second element,
etc.

This statement accesses the value of the first element [0] in myNumbers:
Example
int myNumbers[] = {25, 50, 75, 100};
printf("%d", myNumbers[0]);

// Outputs 25

Try it Yourself »

Change an Array Element


To change the value of a specific element, refer to the index number:

Example
myNumbers[0] = 33;

Example
int myNumbers[] = {25, 50, 75, 100};
myNumbers[0] = 33;

printf("%d", myNumbers[0]);

// Now outputs 33 instead of 25

Try it Yourself »

ADVERTISEMENT

Loop Through an Array


You can loop through the array elements with the for loop.
The following example outputs all elements in the myNumbers array:

Example
int myNumbers[] = {25, 50, 75, 100};
int i;

for (i = 0; i < 4; i++) {


printf("%d\n", myNumbers[i]);
}

Try it Yourself »

Set Array Size


Another common way to create arrays, is to specify the size of the array, and
add elements later:

Example
// Declare an array of four integers:
int myNumbers[4];

// Add elements
myNumbers[0] = 25;
myNumbers[1] = 50;
myNumbers[2] = 75;
myNumbers[3] = 100;

Try it Yourself »

Using this method, you should know the number of array elements in
advance, in order for the program to store enough memory.

You are not able to change the size of the array after creation.
Example 1: Array Input/Output
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

#include <stdio.h>

int main() {

int values[5];

printf("Enter 5 integers: ");

// taking input and storing it in an array


for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}

printf("Displaying integers: ");

// printing elements of an array


for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}
Run Code

Output

Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3

Here, we have used a for loop to take 5 inputs from the user and store them
in an array. Then, using another for loop, these elements are displayed on
the screen.

Example 2: Calculate Average


// Program to find the average of n numbers using arrays

#include <stdio.h>

int main() {

int marks[10], i, n, sum = 0;


double average;

printf("Enter number of elements: ");


scanf("%d", &n);

for(i=0; i < n; ++i) {


printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);

// adding integers entered by the user to the sum variable


sum += marks[i];
}

// explicitly convert sum to double


// then calculate average
average = (double) sum / n;

printf("Average = %.2lf", average);


return 0;
}
Run Code

Output

Enter number of elements: 5


Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39.60

Here, we have computed the average of n numbers entered by the user.

You might also like