Practical Application for C Programming: Creating Functions

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

In this practical application lesson, you will write working C code. You will declare and define functions that do not return a value, and functions that accept parameters and return a value

Lesson Overview & Knowledge Required

By the time you have completed this lesson, you will be able to declare and define functions in C. You will also be able to write a function that has no return value (void), and one that can accept parameters and/or return values back to the caller.

A basic understanding of the C language is required, and how to compile and debug simple C programs with an empty main() function within an integrated development environment (IDE).

Program Code

In order to review functions in C programming, let's look at a couple of things. One thing is that we can pass parameters to a function if we wish to, or simply call the function by itself (with no parameters). The other thing is that a function can return a result of some kind to the caller, or simply return to the caller and return nothing at all.

Parameter Return Types

A function in C can accept values, and these are called parameters. It can also return values back to the caller (the code that invoked the function). But there is no law stating that a function must use parameters or return anything.

Functions without Parameters or Return Values

A function can return void, meaning not return any values. In this case, it makes sense to declare a function of type void, to denote that the function does not return anything. In traditional C (as opposed to C++), if the function has no parameters, we also need to say that. For example:

void function(void) {}

These days, it is rare to see the word void inside the function parameter list, as most compilers don't mind if we just leave it blank, same as in C++.

Functions with Parameters and Return Values

Or, you can create a function that accepts parameters (or not), does something (or not), and then returns a value back to the caller (or not). The following example accepts parameters and returns a value.

int function(in a, int b) {}

Program Code

For our code we are going to create a function with a void return value, meaning nothing is returned. In our example it is a simple logo or banner that we want to display. We will add more complexity as we go.

#include <stdio.h>
//function declaration
void printLogo(void);
//main function
int main() {
 //call the function
 printLogo(); 
 return 0;
}
//function definition
void printLogo(void) {
 printf("************************************************\n");
 printf("*********** PARKER'S PITCHING STATS ************\n");
 printf("************************************************\n");
}

Parameters

If you want to pass parameters to a function and return a value, you'll need a type other than void. Consider that we want to calculate batting average by sending the at-bats and hits to a function.

The following code casts integer values to floating point values, since we need to use division to figure out a batting average. Since baseball stat folks talk in whole numbers, even for a percentage value, we will multiply the average by 1,000 after we are done with our floating point stuff. So we can say, "Betts hit an average of 346"

Note the function declaration and the details in the function definition. The function also returns the calculated average back to the caller, in this case the print statement.

#include <stdio.h>
#include <math.h>
//function declaration
float batting_avg(int h, int a);
int main() {
 char batter[] = "Betts";
 int hits = 180;
 int atbats = 520;
 //print and call the batting-avg function
 printf("Batting Average for Betts is: %.0f", batting_avg(hits, atbats));
 return 0;
}
//function definition
float batting_avg(int h, int a) {
 int hits = h;
 int atbats = a;
 float avg = ((float)hits / (float)atbats) * 1000;
 //return the average back
 return avg;
}

Code Application

Now that we've covered the basics, it's your turn to write C program code! The code that follows is only partially completed. You will need to complete the following to get it to work:

  • Complete the calcERA function to calculate the ERA (ERA is calculated by earned runs * innings, divided by innings pitched)
  • Return the ERA value to the calling function (main)
  • In main, call the function
#include <stdio.h>
//function declaration
void printLogo();
float calcERA(int e, int i, int ip);
int main() {
 //call the function
 printLogo(); 
 int er;
 int ip;
 int innings;
 float era;
 printf("Enter Earned Runs: ");
 fflush(stdout);
 scanf("%d", &er);
 printf("\nEnter Total Innings: ");
 fflush(stdout);
 scanf("%d", &innings);
 printf("\nEnter Innings Pitched: ");
 fflush(stdout);
 scanf("%d", &ip);
 return 0;
}
//need function definition
//ERA = ((Earned Runs * Innings) / Total Innings Pitched)

Follow-Up Questions

Now that you have written C code to process a function, answer the following questions to further your understanding.

  • Consider the following code. What will happen if you compile this code?

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account
Support