Skip to content

An example which explains pass by reference to python users

Notifications You must be signed in to change notification settings

hardingprofessional/c_add

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

Pass By Reference

C and Fortran are quite old languages. Most modern languages exist in a function based paradigm. The idea is as follows: Given a function 𝑓(𝑥)=𝑦 , your programming functions should accept an input x and return an output 𝑦.

In C and Fortran, this behavior is the exception, not the norm. Instead, it was quite common to provide the address of where the function should write its results. This is more of a subroutine based paradigm.

I wrote c_add to to highlight these differences so that python programmers can better understand the behavior they see while using the ctypes module.

This code was created in conjunction with this blog post.

Code Explanation

The add_fun function behaves like a modern programming function. It has two inputs, it computes and returns an output.

The add_ref structure is very different. The memory addresses of the three numbers are passed to the function, then they are directly modified using the de-reference operator, *.

About

An example which explains pass by reference to python users

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages