- As we continue our discussion of functions,
we are now going to talk about
passing variables to functions.
What does it mean?
It means that the function can work and do its magic
using values it receives externally.
Functions are just mini programs,
which may or may not use external data.
Parameters are those variables that are defined to accept
external data.
And arguments are the data passed onto the function.
We'll see an example shortly.
Here's an example of a function without parameters.
In this case, the function parameters section
simply states "void".
Everything the function needs
is contained within the functions.
Everything it does, it's calculated internally
and it prints the result.
The way to call this function is simply use its name
in parenthesis.
The second example, it's a functions that does use parameters.
In this case, there's two of them defined
as float x and float y.
The function performs a calculation with those two values
and then it prints out the result.
Note that the function is being called
with two numbers, 2.6 and 4.2.
Those numbers are passed accordingly
and mapped onto the two variables
that are the parameters of the functions.
When the function is called, the 2.6 and 4.2
are called the arguments.
When the function is defined, the float x and float y
are called the parameters of the function.
We are going to look at the c program
and see an implementation of those function
with and without parameters.
This is a scenario in which the user interacts
with an ATM like program,
and has an opportunity to either debit or credit an account,
and a few other things would happen
during those transaction which you'll see right now.
Let's take a look at the main program first
and then we'll see what we needed to define for that.
The first thing that program does
is to initialize an account and print the balance.
This is performed in lines 30 and 31
calling the function getBalance.
Then the customer will perform a transaction,
update the account, get the balance,
perform a second transaction, update the account again,
and on line 39, there even a gift added.
What I would like you to notice in this example
is the fact that, we are repeatedly calling
the same functions.
For example, askCustomer, line 33, line 37, and line 42.
getBalance, line 31, line 35, line 40 and line 45.
This is possible due to our approach to programming
which we called earlier, functional programming.
By defining the set of function as short blocks of code,
tasked with a very particular item that they have to calculate
or a particular action that they have to perform,
it enables us to keep reusing that block of code
by simply calling its name.
For example, askCustomer, it simply displays the message,
next transaction, enter the amount, credit, debit,
positive, or negative, then ask the user the amount.
Each time we need to ask the customer to enter this amount,
we simply call the function askCustomer.
Let's look at the function addGift.
This function is using the parameter, which is a float
and it's the gift amount.
When the function receives an argument
for example, five dollars as you have seen in the main,
it's being translated into gift amount.
The gift amount is now an internal variable
whose value is only known inside the function.
It's being added to account balance in line 54
and the message is being displayed to the user.
Another function that is using the parameter
is on line 67, the function updateAccount.
The account is updated with the amount
that the user has entered in another function askCustomer.
There are different ways to interact with functions
via passing a variable.
As we go along we will keep learning more and more
interesting ways and ways that gives you a lot
of flexibility in the way you write your code
and write your functions.
Let's run this program and see how it works.
First the program tells us this the current balance is zero
and we are suppose to enter a first transaction.
Either positive or negative.
I'm going to enter "100".
The account was updated with 100 dollars
and the current balance in the account is 100.
Let's see where this comes from.
We just performed the first transaction.
I was asked how much money I want to debit or credit.
The account was updated and the balance was printed.
Let's proceed, the current balance is 100,
next transaction, this time I would like to debit my account.
It has been debited by 23 dollars and as you can see
in the code, a gift in line 39 has been added
in the amount of five dollars,
then indeed the addGift function also includes a note,
"Congratulations! A fit of $5.00
"has been added to your account".
The current balance now aggregating my debit
and the gift is 82 dollars.
I am being asked to perform one more transaction.
I will debit yet another amount, let's say 89 dollars,
actually I'm going to put it negative.
A gift has been added of two dollars.
The balance in my account is now -5 dollars
and I have no more transactions.
To recap, you can see that our program
are started to get longer and longer and more complex,
even more making the case for using functions
as much as possible
because if we didn't have functions
each time you see a call to this function
I would've had to copy this code again and again.
This program might have become even longer.
Furthermore, should I want to change
the message that I'm asking the customer to do,
the instructions to the customer,
a congratulatory note,
anything in any functions, the moment I change
into that function, it's being updated each time
the function is being used.
Again, this is the beauty of functional programming
and it's enhanced by the use of parameters.
In the upcoming videos, we will do even more complex
and interesting things with passing parameters.