09 - Functions

A function is a block of code that performs a set of commands. This block of code can be called from your main, and can be reused over and over again.

// At the top, before the main(), you create/declare the function

void function_name(void)

{

// commands that the function

// will execute go in here

}

// Then inside the main() you can call upon the function whenever you want

function_name();

A function starts with a return type, then the name, and the input parameters in parenthesis. Right now, the return type and parameters are void since we are not passing in anything, and not returning anything. Void means that there will be no inputs at all and nothing will be sent out. We will learn about return types and parameters later.

To call a function, you type the name of the function in your main followed by parenthesis. In C and Java, a name without parenthesis is a variable and a name with parenthesis is a function. Example:

function_name; // This calls on a variable named "function_name"

function_name(); // This calls on a function named "function_name"

Below is an example of a function that counts from 1 to 10. The function is being called from the main. This function does not take in any inputs and does not send back any return value or information. It simply runs 10 printf commands.

Activity 1

Create a function called printHello.

This function will not have any inputs and will not have any return value.

This function will print "Hello!".

Call this function in the main() at least twice.

Solution: Link

Activity 2

Create a function called count_down.

This function will not take in any inputs and will not have any return value.

This function will count down from 30 to 5 by 5s using a loop.

Call this function in the main().

Solution: Link

Inputs (aka Parameters or Arguments)

Input parameters are how you pass information into a function. You can have no input parameters, you can have one input parameter, or you can have as many input parameters as you like. For each input parameter you need to give it a data type and a name. If you pass in multiple inputs, then each input will be separated by a comma. Input parameters have the following format:

// In this function, you are taking in a single integer value as an input

void function_name(int a)

// You are creating a local variable called "a" that will only exist inside of your function

// "a" will have the value of whatever you passed in, but any changes you make to "a" will not affect the variable you passed in

// In this function, you are taking in two integer values as an inputs

void function_name(int input1, int input2)

// You are creating two local variables called "input1" and "input2" that you can use inside of this function

Below are a few examples of functions with input parameters.

Activity 3

Create a function called "Greeting"

This function takes in a single string input (the user's name) and does not return a value.

This function will print "Hello <name>! Welcome to the world of FUNCTIONS!".

In the main body of code, ask the user for their name.

Call on the "Greeting" function and pass in the user's name as the one input.

Hint: Just like you create an integer with "int a" you will create a string in the function input as "char name[100]" and then refer to it inside the function as a "%s" string called "name"

Solution: Link

Activity 4

Create two separate functions:

The first one is called "CountUp1". This function takes in a single integer "a" and counts from 1 up to "a" by 1's.

The second one is called "CountDown3". This function takes in two integers "a" and "b". It counts down from "a" to by "b" by 3's.

Call CountUp1() in your main body of code, and pass in 5 for a.

Call CountUp1() in your main body of code, and pass in 11 for a.

Call CountDown3() in your main body of code, and pass in 10 and 1 for 'a' and 'b' respectively.

Call CountDown3() in your main body of code, and pass in -2 and -23 for 'a' and 'b' respectively.

Solution: Link

Activity 5

Create a function called "CountingFunction"

This function will take in three integer-type input parameters but will not return any value.

Have the function start at the first value (int start) and count to the second value (int end) by multiples of the third value (int multiples).

Be able to handle counting up or down depending on which is greater, the starting value or ending value.

Ask the user for 3 integer inputs (as described above).

Call on your function, passing in the 3 user-inputted integers as the inputs.

Solution: Link

Return Values

Return values are the information your function send back as output. A function should only ever send out a single output. If you want to send out more than one piece of information, you would send out a single array/list. The data type of the return value is clearly stated before the name of the function. Inside the function, you will clearly state what value is returned.

// This function will return a single integer value. That is what the "int" before the function name means.

int doubleNumber(int a)

{

int newNumber = a * 2;

return newNumber; // Here is where the value is actually returned.

}

A Few Important Notes:

    1. If you have a void return type, the function does not need to return anything. Hence you do not need the last line stating "return <value>;"

    2. The value you return must be of the same data type that is stated at the beginning.

    3. Once a function reaches a "return <value>;" statement, the function ends. There may be many of these return statements in your function, but only one will ever be executed. Example: you could have an IF ELSE condition inside the function that says IF (condition) then return 5, ELSE return 7. Only one of those will be executed, and then the function will end, even if there are lines of code below it.

Using A Return Value

When you call a function that has a return value, make sure to store the result or use it somehow.

int result;

result = doubleNumber(5); //The result of the doubleNumber(5) is then stored into the result variable.

A very common mistake for beginners is to have a function run a few lines of code and return a value, but then simply call on the function as below:

// Though this function may work completely properly and double the number and return a value of 10 ... you didn't use it.

doubleNumber(5);

// The previous line of code is the same as writing this:

10;

// Instead you need to either save the value or use the value

int result = doubleNumber(5);

printf("The number 5 doubled is %d", doubleNumber(5) );

The following example shows the use of return types.

Activity 6

Write a function called "tripleNumber".

It will take in an integer "a" and return the value of "a" times 3.

In your main body of code, ask the user for an integer.

Call on the tripleNumber() function, passing in the user's integer as its input.

Save the value returned as "result".

Print the result in a complete sentence.

Solution: Link

Activity 7

Write 3 functions.

    1. double areaSquare(double side) - Takes in the length of a side of a square, and returns the area of the square.

    2. double areaCircle(double radius) - Takes in the radius of a circle, and returns the area of the circle. Use 3.14159 for pi.

    3. double areaTriangle(double base, double height) - Takes in the base and height of a triangle, and returns the area of the triangle.

In your main body of code, using complete sentences:

    1. Ask the user for the length of the side of a square. Call on the "areaSquare" function. Print the result in a complete sentence.

    2. Ask the user for the radius of a circle. Call on the "areaCircle" function. Print the result in a complete sentence.

    3. Ask the user for the base and height of a triangle. Call on the "areaTriangle" function. Print the result in a complete sentence.

Solution: Link

Common Mistakes

Coming Soon - Make some mistakes =)