One Of Transact Sql Forum Topic i was see
 Problems using functions in sql server            

(link is that
How to execute dynamic sql from function
)
 
and i was think about this solutions

actually in sql server have limitations in functions

one of the limitations of custom functions
with  parameter  can not be transferred  name of the table function

for example we have 20 tables that we need to work 20 functions
for this problems i was  think about " xp_exec"

for example first i will created table and dynamically will select columns from this table

use tempdb
go
create table test
(t1 int)
go
insert test values(1)
insert test values(2)
insert test values(2)
insert test values(3)
insert test values(3)
insert test values(4)
insert test values(5)
insert test values(8)
 
go
and then we will create a function 
for given results dynamically

drop function showfunction
go
create function dbo.showfunction(@tablename sysname,@colname sysname)
returns @showtable table (showresult sql_variant)
as
begin
declare @stringsql varchar(8000)
set @stringsql='
Create Function dbo.showprim()
Returns @showtab Table(showres sql_variant)
as
begin
insert @showtab
Select showres=AVG(t1) from (
select min(t1) as t1 from (
select top 50 percent '+@colname+' as t1 from '+@tablename+' order by t1 desc
)t
union all
select max(t1) from (
select top 50 percent '+@colname+' as t1 from '+@tablename+' order by t1
)t
)M
Return
end
'
 
exec master..xp_exec 'Drop Function Dbo.showprim','N','tempdb'
exec master..xp_exec @stringsql,'N','tempdb'
Insert @showtable Select*from showprim()
return
end
go
 
select* from showfunction('test','t1')
go
drop table test
 
showresult
----------------
3
and results was success