The name of a function identifies it and must be used when the function is called.

The name of a function identifies it and must be used when the function is called.

In programming, a function is a reusable block of code that executes a certain functionality when it is called.

Functions are integral parts of every programming language because they help make your code more modular and reusable.

In this article, I will show you how to define a function in Python and call it, so you can break down the code of your Python applications into smaller chunks.

I will also show you how arguments and the return keyword works in Python functions.

Basic Syntax for Defining a Function in Python

In Python, you define a function with the def keyword, then write the function identifier (name) followed by parentheses and a colon.

The next thing you have to do is make sure you indent with a tab or 4 spaces, and then specify what you want the function to do for you.

def functionName():
    # What to make the function do

Basic Examples of a Function in Python

Following the basic syntax above, an example of a basic Python function printing “Hello World” to the terminal looks like this:

def myfunction():
    print("Hello World")

To call this function, write the name of the function followed by parentheses:

myfunction()

Next, run your code in the terminal by typing python filename.py to show what you want the function to do:

The name of a function identifies it and must be used when the function is called.

Another basic example of subtractig 2 numbers looks like this:

def subtractNum():
    print(34 - 4)

subtractNum()
# Output: 30

Arguments in Python Functions

While defining a function in Python, you can pass argument(s) into the function by putting them inside the parenthesis.

The basic syntax for doing this looks as shown below:

def functionName(arg1, arg2):
    # What to do with function
    

When the function is called, then you need to specify a value for the arguments:

functionName(valueForArg1, valueForArg2)

Here's an example of arguments in a Python function:

def addNum(num1, num2):
    print(num1 + num2)
addNum(2, 4)

# Output: 6

In the example above:

  • I passed 2 arguments into the function named addNum
  • I told it to print the sum of the 2 arguments to the terminal
  • I then called it with the values for the 2 arguments specified

N.B.: You can specify as many arguments as you want.

How to Use the Return Keyword in Python

In Python, you can use the return keyword to exit a function so it goes back to where it was called. That is, send something out of the function.

The return statement can contain an expression to execute once the function is called.

The example below demonstrates how the return keyword works in Python:

def multiplyNum(num1):
    return num1 * 8

result = multiplyNum(8)
print(result)

# Output: 64

What’s the code above doing?

  • I defined a function named multiplyNum and passed it num1 as an argument
  • Inside the function, I used the return keyword to specify that I want num1 to be multiplied by 8
  • After that, I called the function, passed 8 into it as the value for the num1 argument, and assigned the function call to a variable I named result
  • With the result variable, I was able to print what I intended to do with the function to the terminal

Conclusion

In this article, you learned how to define and call functions in Python. You also learned how to pass arguments into a function and use the return keyword, so you can be more creative with the functions you write.

If you find this article helpful, don’t hesitate to share it with your friends and family.



Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

C Identifiers

  • Article
  • 08/03/2021
  • 3 minutes to read

In this article

"Identifiers" or "symbols" are the names you supply for variables, types, functions, and labels in your program. Identifier names must differ in spelling and case from any keywords. You cannot use keywords (either C or Microsoft) as identifiers; they are reserved for special use. You create an identifier by specifying it in the declaration of a variable, type, or function. In this example, result is an identifier for an integer variable, and main and printf are identifier names for functions.

#include <stdio.h>

int main()
{
    int result;

    if ( result != 0 )
        printf_s( "Bad file handle\n" );
}

Once declared, you can use the identifier in later program statements to refer to the associated value.

A special kind of identifier, called a statement label, can be used in goto statements. (Declarations are described in Declarations and Types Statement labels are described in The goto and Labeled Statements.)

Syntax

identifier:
    nondigit
    identifier nondigit
    identifier digit

nondigit: one of
    _ a b c d e f g h i j k l mn o p q r s t u v w x y z
    A B C D E F G H I J K L MN O P Q R S T U V W X Y Z

digit: one of
    0 1 2 3 4 5 6 7 8 9

The first character of an identifier name must be a nondigit (that is, the first character must be an underscore or an uppercase or lowercase letter). ANSI allows six significant characters in an external identifier's name and 31 for names of internal (within a function) identifiers. External identifiers (ones declared at global scope or declared with storage class extern) may be subject to additional naming restrictions because these identifiers have to be processed by other software such as linkers.

Microsoft Specific

Although ANSI allows 6 significant characters in external identifier names and 31 for names of internal (within a function) identifiers, the Microsoft C compiler allows 247 characters in an internal or external identifier name. If you aren't concerned with ANSI compatibility, you can modify this default to a smaller or larger number using the /H (restrict length of external names) option.

END Microsoft Specific

The C compiler considers uppercase and lowercase letters to be distinct characters. This feature, called "case sensitivity," enables you to create distinct identifiers that have the same spelling but different cases for one or more of the letters. For example, each of the following identifiers is unique:

add
ADD
Add
aDD

Microsoft Specific

Do not select names for identifiers that begin with two underscores or with an underscore followed by an uppercase letter. The ANSI C standard allows identifier names that begin with these character combinations to be reserved for compiler use. Identifiers with file-level scope should also not be named with an underscore and a lowercase letter as the first two letters. Identifier names that begin with these characters are also reserved. By convention, Microsoft uses an underscore and an uppercase letter to begin macro names and double underscores for Microsoft-specific keyword names. To avoid any naming conflicts, always select identifier names that do not begin with one or two underscores, or names that begin with an underscore followed by an uppercase letter.

END Microsoft Specific

The following are examples of valid identifiers that conform to either ANSI or Microsoft naming restrictions:

j
count
temp1
top_of_page
skip12
LastNum

Microsoft Specific

Although identifiers in source files are case sensitive by default, symbols in object files are not. Microsoft C treats identifiers within a compilation unit as case sensitive.

The Microsoft linker is case sensitive. You must specify all identifiers consistently according to case.

The "source character set" is the set of legal characters that can appear in source files. For Microsoft C, the source set is the standard ASCII character set. The source character set and execution character set include the ASCII characters used as escape sequences. See Character Constants for information about the execution character set.

END Microsoft Specific

An identifier has "scope," which is the region of the program in which it is known, and "linkage," which determines whether the same name in another scope refers to the same identifier. These topics are explained in Lifetime, Scope, Visibility, and Linkage.

See also

Elements of C

Feedback

Submit and view feedback for

When a function is called it is?

Now, whenever a function is called a new stack frame is created with all the function's data and this stack frame is pushed in the program stack, and the stack pointer that always points the top of the program stack points the stack frame pushed as it is on the top of the program stack.

What term is used for the name used inside a function to refer to the value passed as an argument?

parameter. A name used inside a function to refer to the value passed as an argument. stack diagram. A graphical representation of a stack of functions, their variables, and the values to which they refer.

What is another term for parameters of a function when it is called?

The term parameter (sometimes called formal parameter) is often used to refer to the variable as found in the function definition, while argument (sometimes called actual parameter) refers to the actual input supplied at function call.

What is named function in JavaScript?

Named functions in JavaScript is just a fancy way to refer to a function that uses the function keyword and then a name you can use as a reference to that function.