Category: Functions in C

Coding Standards for Writing Functions 1

Coding Standards for Writing Functions

Whether its C programming language or C++ programming language or PHP, we should have a coding standard in place before starting with the code development. Coding standard is a predefined standard which should be followed during the code development which will improve the quality of the code. The coding standards varies from organization to organization. Each organization define their own coding standard and follow them to achieve the desired code quality. Here, we will define a coding standard for writing functions. Coding Standards for Writing Functions A function name should be preceded by fn The first character in the function...

How does using functions make writing large programs easier? 1

How does using functions make writing large programs easier?

Consider a program with 1000 lines. It is much easier to write a properly designed program consisting of 20 50-line functions than it is to write a program consisting of one 1000-line main program. Functions can be written, tested, and understood independently of one another. You can write a function, test it, and convince yourself that it is correct without worrying about the other functions in your program. It is much easier to get your mind around a 50-line thing than a 1000-line thing. Another benefit is that code can be reused between projects. If someone has previously written a...

Function Prototype in C Language 7

Function Prototype in C Language

As we all know, before using a variable we need to declare it. Similarly, before using a function we need to declare the function. This declaration of the function is called as function prototyping. Consider the below program. float fnGetAverage(int, int); void main() { int x,y; float z; printf(“\nEnter the values for x and y”); scanf(“%d %d”, &x, &y); z = fnGetAverage(x, y); //function call printf(“\nThe average is %d”, z); } float fnGetAverage(int a, int b) { //function definition float c; c = (a+b)/2; //calculating average return c; //returning the value } There is one statement before the main() function....

Function declaration and Prototype in C Programming Language 19

Function declaration and Prototype in C Programming Language

By default, any C function returns an int value. If we want that the function should return a value other than int then we need to explicitly mention it in the function definition. Let us understand this using an example. Example 1: float fnGetAverage(int, int); //function prototype void main() { int x,y; float z; printf(“\nEnter the values for x and y\n”); scanf(“%d %d”, &x, &y); z = fnGetAverage(x,y); //function call printf(“\nThe average is %f”, z); } float fnGetAverage(int a, int b) { //function definition float c; c = (a+b)/2; //calculating average return c; //returning the value } Output: Enter the...

Functions in C 27

Functions in C

Let us understand the concept of functions in C language with the help of day to day scenario. Let us suppose that we want to write a program that will perform all the operations required for banking. So we will have to write a program that will have following steps: Accept account number and other personal details of the customer as a input Accept the details of the transactions to be performed If the customer wants to withdraw the money, then program should fetch the balance amount and perform eligibility check After performing eligibility check, the money is deducted from...

FREE C Cheatsheet - Speed Up Your C Programming.

FREE C Cheatsheet - Speed Up Your C Programming.

Download a 7-page free cheat sheet for easy and quick access to C Concepts, Snippets, and Syntax.

Thank you! Check you inbox and access your cheat-sheet