Site icon TestingDocs.com

C Functions

Introduction

In this tutorial, we will learn about C Functions. Complex and large C programs can be divided into smaller functions that perform a certain task.

What is a Function?

A function is a reusable block of code that has a name. The function can be invoked from other parts of the program. In the C language, the main() itself is a function that is the main program entry point and is invoked by the operating system. The main() can invoke the function to execute it.

A function is a self-contained block of statements that performs a specific task (creates a side-effect) and may also return a value to the caller.

C Function = side effect + return value to the caller

Structure of C Function

In order to make use of the function, you have to do three steps. The three steps are:

To know more about the structure of the C function with an example:

Structure of C Function

Sample C Program

Let’s see how to perform those steps with the help of a simple C program. The sample program is to calculate the Total, Average, and Percentage of subject marks of a student.

 

#include <stdio.h>
// Sample Program to calculate Total, Average and Percentage of 
// subject marks

//Function prototypes as per the Question
float getStudentMarks(int n,int maxMarks);
float calculateAverage(int n,float total);

/*******************************
* Main method
********************************/
int main(){
// to hold n
int n;
float totalMarks=0.0;
float averageMarks=0.0;
float percentageOfMarks=0.0;
// max marks for each subject, usually 100.
int subjectMarksUpon=100;

printf("Enter number of subjects: \n");
scanf("%d", &n);
totalMarks=getStudentMarks(n,subjectMarksUpon);
averageMarks = calculateAverage(n,totalMarks);
percentageOfMarks = (totalMarks/(n * subjectMarksUpon)) * 100;

printf("\nTotal of %d subjects marks = %0.2f",n,totalMarks);
printf("\nAverage Marks = %.2f", averageMarks);
printf("\nPercentage = %.2f %%", percentageOfMarks);
return 0;
}

/*******************************
* getStudentMarks() method
********************************/
float getStudentMarks(int n,int maxMarks){
int i;
float marks=0.0;
float total=0.0;
for(i = 0; i < n; i++){
printf("Enter marks of each subject:\n");
scanf("%f", &marks);
printf("You entered = %.2f / %d \n",marks,maxMarks);
total += marks;
}
return total;
}

/*******************************
* calculateAverage() method
********************************/
float calculateAverage(int n,float total){
return total/n;
}

Program Output

Enter number of subjects:

3

Enter marks of each subject:

56

You entered = 56.00 / 100

Enter marks of each subject:

78

You entered = 78.00 / 100

Enter marks of each subject: 88

You entered = 88.00 / 100

Total of 3 subjects marks = 222.00

Average Marks = 74.00

Percentage = 74.00 %

The IDE used in the tutorial is Code:: Blocks.

To download and install Code Blocks follow the link:

https://www.testingdocs.com/download-and-install-codeblocks/

For more information on Code Blocks IDE, visit the official website of Code blocks IDE: http://www.codeblocks.org/

Exit mobile version