Functions are among the most important pieces of programming. In fact, the paradigm of programming that this site presents in known as procedural programming. Procedures are functions. Unfortunately, advances in programming development have taken the term functional programming and has provided a more technical meaning. Functions are a way for programs and programmers to modularize code. Functions provide a way to call on a set of actions through a trigger word (a function name).
Functions have a header and then a body. We will go into a bit of detail and be ready to note differences between the three similar languages and Python.
returnType functionName(parameterList) {
//function body
}
Let's go through the elements:
returnType
functionName
parameterList
Functions that are programmer-defined are intended to be called as well. To call the function, the name of the function and the type of argument the function is required to have are requisite to have the function operating as expected. Functions are always called from other functions. Always remember that we are always running a main function. In Python we will create our main in order to distinguish our other supportive functions.
Void functions are the simplest types of function to define. They operate on the assumption that the body will be executed with nothing to return. Let's use the letter grade program that we worked on in the decision structures page. We will put the decision structure in the function and then see how to make this work.
Language | Code |
---|---|
C++ |
#include <iostream>
|
C# |
using System;
|
Java |
import java.util.Scanner;
|
Python |
def getLetterGrade(g):
|
Functions are what what we make them. The programmer determines what is required for the function to work. We realize then the in this particular program has a programmer-defined function getLetterGrade
that has a parameter g
. The parameter is additional data that is being used to navigate the function definition. When we call the function (in main) we have to provide the name of the function and an argument (in this case grade
) to satisfy the definition we came up with.
All programming languages begin from main. The more advanced you get, you will style programs with main
written first then the other functions afterwards. The main function/method will execute to the function call and then surrender control to the defined function until it is complete and then resume where it left off.
Void functions are strictly for processing actions only.
A value-returning function is one where the goal is to perform some action and deliver a value back to the function that called it. In void functions, we typically see a lot of output statements to end the function because it does not send information back. Value-returning functions send information and thus instead of outputs, we define the function to isolate the important value that we want passed back. Functions should be defined with consideration of the law of least principle that states that a function should only do one major logical task. This means that your function should not try to solve a myriad of problems. Only main has the privilege of doing more than one logic action.
With walue-returning functions we now need to really worry about return types. Return types are the data type of the information that will be given back by the function. When we work on the programming example, we will return char
for the letter grade. With return types we also have a return
statement in the function body. This is typically the last statement in the definition and ends the function. Multiple return statements are only permitted when decision structures are used in the function definition.
Value-return function calls are allowed to exist anywhere variables can because they hold data. Void functions do not hold data so they are a lot less covert in operation.We have been using functions extensively throughout all the exercises we have done especially in C#, Java, and Python. We used functions like input, print, Console.WriteLine, keyIn.nextInt(),
etc.
Let's convert our code on the left to a value returning function.
Language | Code |
---|---|
C++ |
#include <iostream>
|
C# |
using System;
|
Java |
import java.util.Scanner;
|
Python |
def getLetterGrade(g):
|
Programming note: Notice that Python function definitions do not change but only adds a return statement. Also, note that the function call can occur in the output statement or could be assigned to variable to make the output statement a tad easier.
Programming does not allow functions to be nested. All function are standalone constructs that wait to be called. There are no limits to how many functions can be created amd used. There also no limits on parameters that a function can have. Note that when functions are called, if the function has parameters, the argument values (acutal values from the call) are copied over to the parameters. Functions are the building blocks of programming and will be in existence in classes, advanced programming concepts.