Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9119311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:21:56+00:00 2026-06-17T05:21:56+00:00

I noticed that, in some cases (I am still a beginner at R!), multi

  • 0

I noticed that, in some cases (I am still a beginner at R!), multi lines instructions cannot be merged ‘as is’ into simple lines instructions. Let’s take an example, which is in a lesson I recently had on line:

> make.power <- function(n) {
+     pow <- function(x) {
+         x^n
+         }
+         pow
+     }

Is the definition of the ‘make.power’ function. I can then define the ‘cube’ or the square functions

> cube <- make.power(3)
> square <- make.power(2)

Then, if I try to define make.power on a single line with exactly the very same syntax:

> make.power <- function(n) { pow <- function(x) { x^n } pow }

This will raise an “Unexpected symbol in “make.power …”. This said, the following single line code will work:

> make.power <- function(n) { pow <- function(x) { x^n }}

and, when switched to multi-line, will still work!

> make.power <- function(n) {
+     pow <- function(x) {
+         x^n
+         }
+     }

I must admit that I am quite confused. My questions are the following:

  1. Why can’t I just merge the multi-line code in a single line instruction?
  2. In the first multi-line definition of the function, why should one
    recall the ‘pow’ function right after the function is defined, which
    is obviously of no interest, as the same code without the recalling
    is ok!

Edit: following #Dwin answer, it is true that the following expression:

> make.power <- function(n) { pow <- function(x) { x^n }; pow }

works perfectly. And so does this one:

> make.power <- function(n){; pow <- function(x) {; x^n }; pow }

So it seems that the “;” could be mandatory in some cases (here, between the “}” and second ‘pow’, and not in other cases (between the ‘{‘ and first ‘pow’)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T05:21:57+00:00Added an answer on June 17, 2026 at 5:21 am

    The carriage return (or linefeed) separates commands. (I earlier said it separates ‘expressions’ but in R that is not really the right terminology. “Expressions” in the R-sense are actually separated by commas.) If you insert a semi-colon within a line, you get the same effect. Despite the fact that there is unnecessary, code this still works as an example of that fact:

    make.power <- function(n) { pow <- function(x) { x^n }; pow }
    

    I was rather disappointed that I could not find this stated as such in any help() files I looked at. I thought that by typing ?Syntax I should have found something relevant. The best I could come up with was the help(parse) page, and even there, only by looking at the example would you have been able to deduce this. I guess you were expected to have read “Intro to R” where I’m reasonably sure it must be described. Yep, there it is in section 1.8.

    In response to your Q2: During the process of assignment with “<-” of the inner function(…) to “pow”, the value returned to the outer function call was just the unnamed function rather than anything that had the name “pow”. Which was the point of my comment to your first comment. That’s probably a more subtle point than you would have been expected to have gotten from “Intro to R”. The return function is sometimes needed when an assignment of only a portion of an object was the last command, but in this case the most compact code would have been:

    make.power <- function(n) { function(x) { x^n } }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my WPF application using MVVM I noticed that in some cases the CanExecute
Reading the Eigen library documentation, I noticed that some objects cannot be passed by
I noticed that some enumerations have None as a enumeration member. For example what
I noticed that some stylesheets have something like this: body { font-size: 62.5%/1.2em; }
While reading cocoa tutorials i've noticed that some of the tutorials use AppDelegate and
I have 100 web servers and noticed that some of them have request filtering
I've noticed that on some websites (including SO) the link to the CSS will
I've playing around with linux and noticed that for some mysterious reason commands like
Quick question, I noticed that on some of my header directors I was getting
I noticed that I get too many database hits whilst rendering some complicated pages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.