{"id":24117,"date":"2020-10-03T07:43:18","date_gmt":"2020-10-03T00:43:18","guid":{"rendered":"https:\/\/tom.ji42.com\/?p=24117"},"modified":"2020-10-03T07:43:18","modified_gmt":"2020-10-03T00:43:18","slug":"procedures-vs-functions","status":"publish","type":"post","link":"https:\/\/tom.tomwork.net\/?p=24117","title":{"rendered":"Procedures Vs Functions"},"content":{"rendered":"<p>In\u00a0<a href=\"http:\/\/fox.wikis.com\/wc.dll?Wiki~VisualFoxpro\">Visual Foxpro<\/a>, you can declare procedural routines or\u00a0<a href=\"http:\/\/fox.wikis.com\/wc.dll?Wiki~ObjectOriented\">Object Oriented<\/a>\u00a0methods as either &#8220;PROCEDURE&#8221; or &#8220;FUNCTION&#8221;, and it has no impact on how that routine is handled by VFP, like this:<code><\/code><\/p>\n<pre>PROCEDURE ThisIsMyProcedure\r\n  Messagebox( \"You are in a Procedure.\" )\r\nENDPROC\r\n\r\nFUNCTION ThisIsMyFunction\r\n  Messagebox( \"You are in a Function.\" )\r\nENDFUNC<\/pre>\n<p><!--more--><\/p>\n<hr \/>\n<h2>Differences between calling Procedures and Calling Functions<\/h2>\n<h3>Functions return a result, Procedures do Not<\/h3>\n<p>If a routine does not include a RETURN statement to return a result, it will return .T. by default.<\/p>\n<h3>The calling pattern determines whether a routine is a Procedure or a Function, not its &#8220;definition&#8221;<\/h3>\n<p>Even if your routine is defined as a PROCEDURE, you can still call it like a function, and it *is* a function. If you did not include a RETURN statement, the function will return .T. (as mentioned above). (<a href=\"http:\/\/fox.wikis.com\/wc.dll?Wiki~ObjectOriented\">Object Oriented<\/a>\u00a0methods must always be called &#8220;as functions&#8221;.)<\/p>\n<p>As discussed in\u00a0<a href=\"http:\/\/fox.wikis.com\/wc.dll?Wiki~ParametersByValueOrReference\">Parameters By Value Or Reference<\/a>, the way that you call a procedural routine also determines the handling of the parameters. This can be confusing, even with the information in the previously mentioned article.<\/p>\n<p>Here are your two options:<br \/>\n<b>1) Use &#8220;DO &#8230; WITH&#8221; to call a routine and it will be considered a &#8220;procedure&#8221;.<\/b><br \/>\nParameters will be passed &#8220;by reference&#8221; unless they are surrounded by parentheses, and there is no way to &#8220;explicitly&#8221; pass parameters &#8220;by reference&#8221;.<br \/>\n(Note that\u00a0<a href=\"http:\/\/fox.wikis.com\/wc.dll?Wiki~SetUDFParms\">Set UDFParms<\/a>\u00a0does NOT affect routines called by &#8220;DO &#8230; WITH&#8221;)<\/p>\n<p>For example:<code><\/code><\/p>\n<pre>DO MyRoutine WITH ParamByRef, (ParamByValue)\r\nDO MyRoutine WITH @ThisDoesNotWork<\/pre>\n<p>or<\/p>\n<p><b>2) Omit &#8220;DO&#8221; and &#8220;WITH&#8221;, and surround all parameters in one pair of parentheses, and your routine will be considered a &#8220;function&#8221;.<\/b><br \/>\nParameters will be passed &#8220;by value&#8221; unless:<br \/>\nA) You place an &#8220;@&#8221; in front of the parameter, or<br \/>\nB) You place (additional) parentheses around the parameter<\/p>\n<p>For example:<code><\/code><\/p>\n<pre>SET UDFPARMS TO VALUE\r\nResult = MyRoutine( ParamByVal, @ParamByRef, (otherParamByVal) )\r\nMyRoutine( ParamByVal, @ParamByRef, (otherParamByVal) )\r\nSET UDFPARMS TO REFERENCE\r\nResult = MyRoutine( ParamByRef, @OtherParamByRef, (ParamByVal) )\r\nMyRoutine( ParamByRef, @OtherParamByRef, (ParamByVal) )<\/pre>\n<p>Contributors:\u00a0<a href=\"http:\/\/fox.wikis.com\/wc.dll?Wiki~wgcs\">wgcs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In\u00a0Visual Foxpro, you can declare procedural routines or\u00a0Object Oriented\u00a0methods as either &#8220;PROCEDURE&#8221; or &#8220;FUNCTION&#8221;, and it has no impact on how that routine is handled by VFP, like this: PROCEDURE ThisIsMyProcedure Messagebox( &#8220;You are in a Procedure.&#8221; ) ENDPROC FUNCTION ThisIsMyFunction Messagebox( &#8220;You are in a Function.&#8221; ) ENDFUNC<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[13],"tags":[],"class_list":["post-24117","post","type-post","status-publish","format-standard","hentry","category-13"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6cOVM-6gZ","_links":{"self":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/24117","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=24117"}],"version-history":[{"count":1,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/24117\/revisions"}],"predecessor-version":[{"id":24118,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=\/wp\/v2\/posts\/24117\/revisions\/24118"}],"wp:attachment":[{"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=24117"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=24117"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tom.tomwork.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=24117"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}