What Are Intrinsic Functions?
Intrinsic functions are procedures built directly into the Fortran language and compiler, available without any USE statement or external library, covering numeric operations (SQRT, ABS, MOD, EXP), type conversion (INT, REAL, DBLE), array reductions (SUM, PRODUCT, MAXVAL, MINVAL), and character manipulation (LEN, TRIM, INDEX). Because they are part of the language standard, they are guaranteed to be available and typically compile down to highly optimized machine instructions, sometimes a single CPU instruction for something like SQRT.
Cricket analogy: The standard set of umpiring signals (four, six, out) that every match uses without a rulebook lookup is like intrinsic functions -- available by default, understood everywhere, no extra setup required.
Numeric and Type-Conversion Intrinsics
Common numeric intrinsics include SQRT(x), ABS(x), MOD(a, p) for remainder, MODULO(a, p) for a floor-division-consistent remainder that always has the sign of p, MIN/MAX with any number of arguments, and EXP/LOG/SIN/COS for transcendental math. Type-conversion intrinsics like INT(x) truncate toward zero, NINT(x) rounds to the nearest integer, and REAL(i) or DBLE(i) promote an integer to single or double precision -- choosing the wrong one silently changes numeric results, so MOD versus MODULO for negative operands is a classic source of bugs.
Cricket analogy: Calculating a bowler's economy rate by dividing runs by overs and rounding to two decimals mirrors NINT rounding a real number to the nearest integer for a clean display.
Array Reduction Intrinsics
Array reduction intrinsics collapse an array into a scalar or lower-rank result: SUM and PRODUCT total or multiply all elements, MAXVAL and MINVAL find extreme values, and MAXLOC/MINLOC return the index of those extremes as an integer array. All four accept an optional DIM argument to reduce along just one dimension of a multidimensional array, and an optional MASK argument (a logical array of the same shape) to reduce only over elements satisfying a condition, combining naturally with the WHERE-style masking seen in array operations.
Cricket analogy: Using MAXLOC on an innings' ball-by-ball runs array finds not just the biggest six but exactly which ball it came off, mirroring how MAXLOC returns an index, not just a value.
Character and String Intrinsics
String handling relies on intrinsics like LEN(str) for declared length, LEN_TRIM(str) for length ignoring trailing blanks, TRIM(str) to strip trailing blanks when concatenating, INDEX(str, substr) to find a substring's position, and ADJUSTL/ADJUSTR to shift text to the left or right edge of a fixed-width field. Because Fortran CHARACTER variables are fixed-length and padded with trailing blanks, forgetting TRIM before concatenation is one of the most common beginner mistakes, producing strings full of unwanted whitespace in the middle.
Cricket analogy: Trimming a scorer's fixed-width 20-character player-name field down to just 'Kohli' before printing a scoreboard headline mirrors TRIM stripping trailing blanks from a CHARACTER variable.
program intrinsics_demo
implicit none
real :: x, y
integer :: a(6), loc(1)
character(len=20) :: name
x = -9.0
y = sqrt(abs(x))
print *, 'sqrt(abs(-9.0)) =', y
print *, 'MOD(-7,3) =', mod(-7, 3), ' MODULO(-7,3) =', modulo(-7, 3)
a = [3, 7, 2, 9, 4, 9]
print *, 'SUM =', sum(a), ' MAXVAL =', maxval(a)
loc = maxloc(a)
print *, 'first index of max value =', loc(1)
name = 'Kohli '
print *, 'trimmed name: [' // trim(name) // ']'
print *, 'position of h in name:', index(name, 'h')
end program intrinsics_demoMOD and MODULO differ when operands have opposite signs: MOD takes the sign of the dividend (MOD(-7,3) = -1) while MODULO takes the sign of the divisor (MODULO(-7,3) = 2). Use MODULO when you need a result consistent with floor division, such as wrapping an index into a valid array range.
- Intrinsic functions are built into the language and require no USE statement or external library.
- Numeric intrinsics include SQRT, ABS, MOD, MODULO, MIN, MAX, EXP, LOG, and the trig functions.
- MOD takes the sign of the dividend; MODULO takes the sign of the divisor -- they differ for negative operands.
- Array reductions (SUM, PRODUCT, MAXVAL, MINVAL, MAXLOC, MINLOC) accept optional DIM and MASK arguments.
- TRIM and LEN_TRIM handle the trailing-blank padding inherent to fixed-length CHARACTER variables.
- INDEX finds a substring's position, and ADJUSTL/ADJUSTR reposition text within a fixed-width field.
- Forgetting TRIM before string concatenation is a very common source of whitespace bugs for beginners.
Practice what you learned
1. What is the key difference between MOD(a,p) and MODULO(a,p)?
2. What does MAXLOC(array) return?
3. Why is TRIM commonly needed before concatenating CHARACTER variables?
4. Which intrinsic function requires no USE statement to call?
5. What does the optional MASK argument do for SUM(array, MASK=condition)?
Was this page helpful?
You May Also Like
Array Operations and Slicing
Whole-array arithmetic, array sections, and the WHERE/FORALL constructs that let Fortran express vectorized operations without explicit loops.
Multidimensional Arrays
How Fortran declares, stores, and loops over arrays with two or more dimensions, and why column-major order shapes fast code.
Formatted I/O
How Fortran's FORMAT edit descriptors give precise, column-aligned control over how numbers and text are printed and read.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics