-2

I am new to programming, and trying out with a little Python3.

I have some trouble understanding the concept behind calling a function? having the defined the following function, what would be the proper way to call it?

def string_length(mystring):
return len(mystring)

Thanks in advance guys

mtolbol
  • 11
  • 1
  • `string_length('hello')` for example – Cory Kramer Feb 02 '18 at 16:34
  • first, your spacing is off. the second line needs to be indented 4 spaces. Second, you'd call it by calling it: "print string_length(this_string)" or "sl = string_length('this')" – mauve Feb 02 '18 at 16:35
  • 2
    [Start with tutorials to understand concepts.](https://www.tutorialspoint.com/python3/python_functions.htm) – Mr. T Feb 02 '18 at 16:35
  • If you are really new to programming maybe a more basic introduction http://www.programmingbasics.org/en/ if the question was only about the syntax of python, sorry for misinterpreting. – RedX Feb 02 '18 at 16:35
  • 2
    Note that `len` is itself a function that you are already calling –  Feb 02 '18 at 16:35
  • 1
    You really need to [read the Python tutorial](https://docs.python.org/3/tutorial/controlflow.html#defining-functions). – Martijn Pieters Feb 02 '18 at 16:36

2 Answers2

1
def string_length(mystring):
    return len(mystring)

print(string_length('something'))

Like that

SuperStew
  • 2,857
  • 2
  • 15
  • 27
  • Thanks mate - what is the purpose of the 'mystring*? that could be anything typed in there, right? by doesn't seem to be used anywhere? – mtolbol Feb 02 '18 at 16:56
  • It's just a variable that's used internally in the function you define. So yes it could be anything you want. If you say `x=5` and then `def some_func(fart)` and the call `some_func(x)`, this tells `some_func` that you want `fart` to be `5` in this case. – SuperStew Feb 02 '18 at 16:59
  • Thanks SuperStew – mtolbol Feb 02 '18 at 17:01
0
length = string_length('some_string')

where length is a variable that will store the output.

Alex Dubrovsky
  • 240
  • 4
  • 14