Functions in C

Lab Overview

In this lab, we will modify our second program and show how to use functions in C to make our code more modular. By doing this, we can make our programs easier to understand and easier to debug and support.

Steps to this lab

The general steps we will follow:

Step 1 - Create the Code

First, copy the following code in the the Jdoodle code editor. Replace any code that is already there.

#include <stdio.h>

int main() {
   // 1 create a string
   char myName[30];

   // 2 prompt for user
   printf("Please enter your name: ");

   // 3 read in the name
   scanf("%s", myName);

   // 4 printf() displays the string inside quotation
   printf("Hello, World, my name is %s", myName);
   printf("!\n");

   return 0;
}

Step 2 - Run the Program

Once the code has been entered into the Jdoodle window, execute the code by clicking on the Execute button.

You should see the following output:

Please enter your name: Eric
Hello, World, my name is Eric!

Step 3 - Create a New Function

Now that we have our program running, we can add a function to our program. In C, you must declare functions before they are called.

Change the code to add a function definition to the top of the program. Also add a call to the function in the main program.

#include <stdio.h>

// 6 new function definition 
void getName() {
  printf("I just got executed!\n");
}

int main() {
   // 1 create a string
   char myName[30];

   // 2 prompt for user
   printf("Please enter your name: ");

   // 3 read in the name
   scanf("%s", myName);

   // 4 printf() displays the string inside quotation
   printf("Hello, World, my name is %s", myName);
   printf("!\n");

   // 5 add in a call to our new function
   getName();

   return 0;
}

Notes:

5 - We added a new function that does not return and data.
6 - We added a function call from the main program to test it out.

When the code is in the editor, save the file to hello.c. To do this,enter Control-x. (do this by pressing down on the control key and then x)

Nano will ask you if you want to save the file, enter Y. It will then ask you to verify the file name. Simply press return. Your file should be ready to compile.

Step 4 - Run the Program

Once the code has been entered into the Jdoodle window, execute the code by clicking on the Execute button.

You should see the following output.

Please enter your name: Eric
Hello, World, my name is Eric!
I just got executed!

Step 5 - Move Code to New Function

Now we will move most of the existing code from main to our function so we can begin making our program more modular. Make the following edits inthe Jdoodle editor.

#include <stdio.h>

// 6 new function definition 
void getName() {

   // 1 create a string
   char myName[30];

   // 2 prompt for user
   printf("Please enter your name: ");

   // 3 read in the name
   scanf("%s", myName);

   // 4 printf() displays the string inside quotation
   printf("Hello, World, my name is %s", myName);
   printf("!\n");

}

int main() {
   char myName[30];

   // 5 add in a call to our new function
   getName();

   return 0;
}

Notes:

1-4 We moved these lines from our main program to our new function, getName().

When the code is in the editor, save the file to hello.c. To do this,enter Control-x. (do this by pressing down on the control key and then x)

Nano will ask you if you want to save the file, enter Y. It will then ask you to verify the file name. Simply press return. Your file should be ready to compile.

Step 6 - Run the Program

Once the code has been entered into the Jdoodle window, execute the code by clicking on the Execute button.

You should see the following output:

Please enter your name: Eric
Hello, World, my name is Eric!

Step 7 - Add One More Function

Now we will add one more function to our program. This time, our function will return a value. In our previous example, the function was of type void, which means the function does not return a value to the main program. In this case, we will ask for the user’s age so we will return and integer value.

Add the following code to the Jdoodle window.

#include <stdio.h>

// 1 new function to get age
int getAge()
{
   int userAge;

   printf("Please enter your age:");
   scanf("%d", &userAge);

   // 2 return age to main program
   return userAge;
}

void getName() {

   char myName[30];

   printf("Please enter your name: ");
   scanf("%s", myName);

   printf("Hello, World, my name is %s", myName);
   printf("!\n");

}

int main() {
   char myName[30];
   // 3 variable for getting the age
   int myAge; 

   // get the users   
   getName();

   // 4 get the age from out new function and prin it out
   myAge = getAge();

   // 5 the %d is used for formatting integers
   printf("My age: %d\n", myAge);

   return 0;
}

Notes:

1 - Added a new function to get the user’s name
2 - Note the return value in the getAge() function
3 - Declare a variable in main to get the user’s age
4 - Call the function to get the name
5 - Print the name using the “%d” mask for integers

Step 8 - Run the Program

Once the code has been entered into the Jdoodle window, execute the code by clicking on the Execute button.

You should see the following output.

Please enter your name: Eric
Hello, World, my name is Eric!
Please enter your age: 21
My age: 21

Summary

We have added a function to handle getting the name from the user to make our code mode modular and easier to maintain. While this example is seems trivial, when program get much bigger, this organization will allow you to handle much more complex logic. It will also make your programs easier to test.

*Note
This lab runs on containers, so as soon as you quit, you will lose your changes. You can save your C program by copying and pasting from the Nano editor if you would like to save this program to your computer.