Functions Calling Functions


#1

I do not understand what the instructions are asking me to do (not clear). I have no idea how to proceed.

https://www.codecademy.com/courses/python-beginner-c7VZg/1/4

def one_good_turn(n):
    return n + 1
    
def deserves_another(n):
    return n + 2


#2

This function makes a call to one_good_turn and adds 2 to the return value
Eg.

def deserves_another(n):
    return one_good_turn(n) + 2

Now we will call this function,

print deserves_another(3)    # 6

#3

Thanks, I wasn't clear as to what was being asked or where to even start for that matter.


#4

In mathematics, this would look like,

g(f(x))

where,

f(x) = x + 1

and,

g(x) = x + 2

only x is f(x).

>>> def f(x):
	return x + 1

>>> def g(x):
	return x + 2

>>> g(f(3))
6
>>>

#5

I suppose I should study math functions a bit more, because that doesn't make sense to me.


#6

A function is an expression, which means it represents a value or reference object such as a list, set or dictionary.

In this case, f(x) (eff of ex, phonetically) is the value,

x + 1

It is this value we are passing into, g(x) (jee of ex) which is now seen as,

x + 1 + 2

#7

In code, obviously a function is more than just the math applications. There are data manipulations, display and output, and so on. But when it comes to math, you can't beat a computer for making the mundane into a greased hog chase.

There is no getting around user inputs. Why else would we bother with an application the user can interact with? This is the first rats nest. You will encounter all of them in time, and devise or discover ways to fact check them.

On paper we can readily discern logical and factual elements. It is that logic which we must now teach the computer to see and disseminate. We are the source of the ideas, and the teachers of how those ideas are turned into logical, ordered steps the computer can follow.

A good lot of coding deals with functions and functional programming, and the industry itself is leaning toward languages that only support a functional program model. Elm and Haskell are two that come to mind, though I have not broken the ice on either of them.

Python lends itself very well to functional programming, but also leaves the door open on other models. At some point you will be required to explore pure functions.

It is with pure functions that we create an environment for our application to play out. A pure function always gives the same result given the same input and never modifies data structures or states. Something in, something out, and always predictible.

Utility functions often don't make any changes to the data, and only output or store things they are handed. They are sort of pure, though not in a definable way. Those functions that manipulate data are not pure.

Math is only one side of information technology. It's the scientific data side of things in a broad sense. Information takes many forms, though, so it's easy to see that there are many other concerns that we can address using a computer and programming.

In other words, don't just bone up on your math prowess, garner as much knowledge and understanding about handling information as you can get your hands on.


#8

That's just what I'll have to do. This coding thing is just not coming to me and resonating with me. I know that I have to think a certain way; but right not I'm having a hard time just remembering the formats for lines of code and how to frame a problem in a coding sense.


#9

Same problem, and still not quite clear. But thanks.


#10

Given this function,

def one_good_turn(x):
    return x + 1

We can write a second function that makes a call to it...

def deserves_another(x):
    return one_good_turn(x) + 2

In both cases, x is a locally defined variable. They are not the same.


#11

I think I got it. Thanks.


#12

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.