简体   繁体   中英

I want to know the working process of this python program

here i called the sm() with arguments a,b but i didn't give any arguments in out()

in my view i'm thinking that a error would be raised. But it works fine

def out():
    c=sm(a,b)  
    print(c)

a=6 
b=8

def sm(a,b):
    return a + b

out()

The way your code currently is, it should work fine without any error. Here the variables a=6 and b=8 are global so if you don't give any arguments to it, it will get the values of a & b from global frame. See step by step Frame and Objects here

def out():
    c=sm(a,b)  # so available here
    print(c)

a=6 # global variable
b=8 # global variable

def sm(a,b):
    return a + b 

out()

Execution Context

执行上下文

基本上在这个程序中你有两个函数out()sm() ,第一个函数sm()接受两个参数a,b作为输入并返回它们的总和作为输出,而其他函数out()只是调用sm()函数输入a,b是全局定义的并将它们的总和存储在c变量中,然后打印给出输出8c变量

This should work fine. Initially, a function named out() is defined. The process starts at initializing a=6 and b=8 globally. Then you have defined sm(a,b) function, nothing happens Then you called the function out() which you have defined earlier. So we got into the function out() which has no parameters. Inside it, you have called function sm(a,b) which you earlier defined. So c gets the value returned by sm(6,8) which is 14. Then prints c. Thus 14 is printed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM