+ 2
Functions in python
Can anyone explain with easy examples so that i can understand two functions easily in a program, also explain return
11 Antworten
+ 4
It sounds like you want to understand callbacks, where functions call functions. Is that right?
+ 3
FAحAD ,
> why are talking about 2 functions? please give some more details.
here is a sample code that uses a function to calculate the average temperature. more details in the sample file:
https://sololearn.com/compiler-playground/ctmKl4OuXwon/?ref=app
+ 3
I understand use of only one function but i got confuse whenever two fuctions come in the same program, do have any example to do same work but with the double function?
+ 3
FAحAD ,
now your question is clear - thanks!
in the attached file you can find some explanations and comments.
https://sololearn.com/compiler-playground/cn3tMJ9dTq7K/?ref=app
+ 2
FAحAD ,
it would be helpful if you could post a code that shows a situation with 2 functions, so that we can see what your issue could be.
+ 2
RandomLearner, Lothar thankyou so much for the explaination, it really help me alot💫🤝
+ 2
Return is returns, or stores a value in data, e.x
In Function
Def num(x,y):
Return x + y
Results = Num(1,2)
Return inserts a value into the result. If you want to be called, use print. If without return, you can just use 'num(1,2)', it's the same as print
+ 1
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def show_temperature(celsius):
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equal to {fahrenheit}°F")
# Calling the function
show_temperature(25)
That coding statement you shared is using a function in a function. You first define the celsius_to_fahrenheit function, and then you defined the show_temperature function. what the show_temperature function does is it takes the celsius from the parameter, and makes a variable called fahrenheit that calls and stores the result from the celsius_to_fahrenheit function using the celcius parameter as its parameter.
Then it prints the original celsius value, is equal to, the fahrenheit value.
Here's a version of the code that might make a little more sense.
#Converts celsius to fahrenheit
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
#Uses the celsius_to_fahrenheit function and prints its result along with the parameter given.
def show_temperature(celsius):
c = celsius
fahrenheit = celsius_to_fahrenheit(c)
print(f"{c}°C is equal to {fahrenheit}°F")
show_temperature(25)
Im not too good at explaining things, but I hope this clears up some confusion.
+ 1
def function_name():
# do something
function_name()
def say_hello():
print("Hello!")
say_hello() # Output: Hello!
def square(num):
return num * num
def double_and_square(x):
doubled = x * 2
return square(doubled)
result = double_and_square(3)
print(result) # Output: 36
0
Yes, actually sololearn confuses me after giving lessons of two functions🙂, i will share
0
Is this enough?
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def show_temperature(celsius):
fahrenheit = celsius_to_fahrenheit(celsius)
print(f"{celsius}°C is equal to {fahrenheit}°F")
# Calling the function
show_temperature(25)